Skip to content

Commit

Permalink
Merge pull request #9 from murageh/refactor#strict-typing
Browse files Browse the repository at this point in the history
Refactor#strict typing
  • Loading branch information
murageh authored Mar 4, 2023
2 parents 32c6ad8 + 82fe032 commit 7f29ba6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const MyFormComponent = () => {
validate={values => {
const errors = {};
if (values.name.length < 1) {
errors.token = 'Enter a name.';
errors.name = 'Enter a name.';
}
return errors;
}}
Expand Down
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.10",
"version": "1.0.11",
"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
11 changes: 10 additions & 1 deletion src/hooks/saveProgress/useProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,26 @@ function useProgress<T extends {}>({
clearFunction,
forceLocalActions = false
}: useProgressProps<T>) {
const [initialized, setInitialized] = React.useState(false);
const [values, setValues] = React.useState(initialValues);

React.useEffect(() => {
if (typeof window !== 'undefined') {
const saved = (storage ?? window.localStorage).getItem(key);
const initialValue: T = JSON.parse(saved!);
let initialValue: T;
try {
initialValue = JSON.parse(saved!);
} catch (e) {
initialValue = {} as T;
}
setValues(initialValue || initialValues || {} as T);
saveValues(values);
setInitialized(true);
}
}, []);

React.useEffect(() => {
if (!initialized) return;
saveValues(values);
}, [values]);

Expand Down

0 comments on commit 7f29ba6

Please sign in to comment.