-
Notifications
You must be signed in to change notification settings - Fork 22
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
Fix/fix development error with react strict mode #32
base: master
Are you sure you want to change the base?
Fix/fix development error with react strict mode #32
Conversation
this works for me, thank you, I believe it should be merged |
Can we merge this please? I would like to have this soon otherwise will have to patch it manually |
tbh I think this PR makes a bunch of changes that are unrelated to the original issue |
return () => { | ||
_mounted.current = false | ||
} |
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.
Does this useEffect need to return this? _mounted.current
is not referenced or otherwise modified by this useEffect
@@ -30,27 +30,29 @@ const useFormPersist = ( | |||
}: FormPersistConfig | |||
) => { | |||
const watchedValues = watch() | |||
const [localExclude] = useState<string[]>(exclude) |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is the memoization of this function worth the overhead cost of useCallback
?
const clearStorage = () => getStorage().removeItem(name) | ||
const getStorage = useCallback(() => storage || window.sessionStorage, [storage]) | ||
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 comment
The 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 getStorage = useCallback(() => storage || window.sessionStorage, [storage]) | ||
const clearStorage = useCallback(() => getStorage().removeItem(name), [getStorage, name]) | ||
const onTimeoutCallback = useCallback(() => onTimeout && onTimeout(), [onTimeout]) | ||
const _mounted = useRef(true) |
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.
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
Hello.
This fixes a bug that happens when using
react-hook-form
withdefaultValues
option.https://beta.reactjs.org/learn/synchronizing-with-effects#how-to-handle-the-effect-firing-twice-in-development
It basically makes sure it only runs once using the good old _mounted ref.
Should fix: #18
I also added a test.