Skip to content

Commit

Permalink
1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
murageh committed Feb 6, 2023
1 parent e0fa161 commit 0f0bdf3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crispice/save-progress",
"version": "1.0.6",
"version": "1.0.7",
"description": "A React hook to save progress in a form, or any other scenario, and restore it when the user returns to the form. It uses localStorage to save the progress.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
12 changes: 4 additions & 8 deletions src/hooks/saveProgress/useSaveProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,26 @@ import React from 'react';
*
* @param key The key to use to save the data in local storage
* @param initialValue The initial value to use if there is no data in local storage
* @param storage The storage to use, defaults to local storage
*
* @returns [values, updateValues, clearValues] The data, a function to update the data, and a function to clear the data
*/
const useSaveProgress = ({key, initialValues, storage}: { key: string, initialValues?: any, storage?: Storage }) => {
const [values, setValues] = React.useState(() => {
const saved = localStorage.getItem(key);
const saved = (storage ?? localStorage).getItem(key);
const initialValue = JSON.parse(saved!);
return initialValue || initialValues || {};
});

// default to local storage
if (!storage) {
storage = localStorage;
}

// Helper function to save the data to local storage
const saveValues = (value: any) => {
storage?.setItem(key, JSON.stringify(value));
(storage ?? localStorage).setItem(key, JSON.stringify(value));
};

// Provide helper function to clear the data from local storage
const clearValues = () => {
setValues(initialValues || {});
storage?.removeItem(key);
(storage ?? localStorage).removeItem(key);
};

// Provide helper function to update the data in local storage
Expand Down

0 comments on commit 0f0bdf3

Please sign in to comment.