-
Notifications
You must be signed in to change notification settings - Fork 22
Fix/fix development error with react strict mode #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { useEffect } from 'react' | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import { SetFieldValue } from 'react-hook-form' | ||
|
||
export interface FormPersistConfig { | ||
|
@@ -30,27 +30,29 @@ const useFormPersist = ( | |
}: FormPersistConfig | ||
) => { | ||
const watchedValues = watch() | ||
const [localExclude] = useState<string[]>(exclude) | ||
|
||
const getStorage = () => storage || window.sessionStorage | ||
|
||
const clearStorage = () => getStorage().removeItem(name) | ||
const getStorage = useCallback(() => storage || window.sessionStorage, [storage]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the memoization of this function worth the overhead cost of |
||
const clearStorage = useCallback(() => getStorage().removeItem(name), [getStorage, name]) | ||
const onTimeoutCallback = useCallback(() => onTimeout && onTimeout(), [onTimeout]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why extract this to another useCallback? Seemed fine as an inline call imo |
||
const _mounted = useRef(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest the value of mounted should be initially false, since we shouldn't consider any component to be mounted until a useEffect runs |
||
|
||
useEffect(() => { | ||
const str = getStorage().getItem(name) | ||
|
||
if (str) { | ||
if (str && _mounted.current) { | ||
const { _timestamp = null, ...values } = JSON.parse(str) | ||
const dataRestored: { [key: string]: any } = {} | ||
const currTimestamp = Date.now() | ||
|
||
if (timeout && (currTimestamp - _timestamp) > timeout) { | ||
onTimeout && onTimeout() | ||
onTimeoutCallback() | ||
clearStorage() | ||
return | ||
} | ||
|
||
Object.keys(values).forEach((key) => { | ||
const shouldSet = !exclude.includes(key) | ||
const shouldSet = !localExclude.includes(key) | ||
if (shouldSet) { | ||
dataRestored[key] = values[key] | ||
setValue(key, values[key], { | ||
|
@@ -65,18 +67,30 @@ const useFormPersist = ( | |
onDataRestored(dataRestored) | ||
} | ||
} | ||
|
||
return () => { | ||
_mounted.current = false | ||
} | ||
}, [ | ||
storage, | ||
name, | ||
onDataRestored, | ||
setValue | ||
setValue, | ||
clearStorage, | ||
dirty, | ||
localExclude, | ||
getStorage, | ||
onTimeoutCallback, | ||
timeout, | ||
touch, | ||
validate | ||
]) | ||
|
||
useEffect(() => { | ||
|
||
const values = exclude.length | ||
const values = localExclude.length | ||
? Object.entries(watchedValues) | ||
.filter(([key]) => !exclude.includes(key)) | ||
.filter(([key]) => !localExclude.includes(key)) | ||
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {}) | ||
: Object.assign({}, watchedValues) | ||
|
||
|
@@ -86,7 +100,17 @@ const useFormPersist = ( | |
} | ||
getStorage().setItem(name, JSON.stringify(values)) | ||
} | ||
}, [watchedValues, timeout]) | ||
|
||
return () => { | ||
_mounted.current = false | ||
} | ||
Comment on lines
+104
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this useEffect need to return this? |
||
}, [ | ||
watchedValues, | ||
timeout, | ||
localExclude, | ||
getStorage, | ||
name, | ||
]) | ||
|
||
return { | ||
clear: () => getStorage().removeItem(name) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for this useState?
By initializing useState with exclude we also lose any reactivity to the value of exclude.