Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

BorjaRafolsMartinez
Copy link

@BorjaRafolsMartinez BorjaRafolsMartinez commented Jan 9, 2023

Hello.

This fixes a bug that happens when using react-hook-form with defaultValues 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.

@BorjaRafolsMartinez
Copy link
Author

@tiaanduplessis

@pedro757
Copy link

this works for me, thank you, I believe it should be merged

@JGJP
Copy link

JGJP commented Feb 12, 2024

Can we merge this please? I would like to have this soon otherwise will have to patch it manually

@JGJP
Copy link

JGJP commented Feb 14, 2024

tbh I think this PR makes a bunch of changes that are unrelated to the original issue

Comment on lines +104 to +106
return () => {
_mounted.current = false
}
Copy link

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)
Copy link

@JGJP JGJP Feb 14, 2024

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])
Copy link

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])
Copy link

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)
Copy link

@JGJP JGJP Feb 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Doesn't seem to work on refresh
3 participants