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

useLocation callbacks not referentially stable #4

Open
Nirei opened this issue May 16, 2022 · 0 comments · May be fixed by #5
Open

useLocation callbacks not referentially stable #4

Nirei opened this issue May 16, 2022 · 0 comments · May be fixed by #5
Assignees
Labels
bug Something isn't working

Comments

@Nirei
Copy link
Collaborator

Nirei commented May 16, 2022

Repeated renders of a component using useLocation return new instances of setContext and setLocation. This makes it very dangerous to use this functions as they can easily trigger re-renders if you are not careful.

See for example this code:

  useEffect(
    () => setContext({ owned, installed }),
    [setContext, owned, installed],
  )

The setContext has been wrapped in a useEffect to prevent continuous triggering of setContext to retrigger full <Routes /> re-render. This fails because the effect is relaunched on every render since setContext changes to a new pointer on each render.

To prevent this, setContext and other router callbacks should be a stable ref.

@Nirei Nirei added the bug Something isn't working label May 16, 2022
@Nirei Nirei self-assigned this May 16, 2022
@Nirei Nirei linked a pull request May 18, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant