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

remove potential race condition updating the ref #15

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

Conversation

timmy-wright
Copy link

Turns out you can assign to react refs during a render cycle, so this PR does that instead of using a layout effect to update the ref. This removes the chance that something happens between the useEvent hook being called and the useLayoutEffect occurring.

@vain0x
Copy link

vain0x commented Feb 6, 2024

// not sure this I like assigning to a ref during the method call, but React recommends it for certain circumstances:
...
ref.current = fn;

According to Caveats of useRef:

  • Do not write or read ref.current during rendering, except for initialization. This makes your component’s behavior unpredictable.

doing ref.current = fn during rendering doesn't seem to be allowed. See also a comment.

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.

2 participants