react-router icon indicating copy to clipboard operation
react-router copied to clipboard

[Bug]: useLocation causes rerenders, memoization impossible because of context.

Open DesertStrom16 opened this issue 2 years ago • 2 comments

What version of React Router are you using?

6.8.0

Steps to Reproduce

Add a useLocation hook to any component, then watch for re-renders with your tool of choice (Why-Did-You-Render in my case).

Expected Behavior

useLocation causes a re-render only when the location has changed.

Actual Behavior

Component re-renders (more than once, meaning not related to Strict Mode) even when location has not changed.

If this is unavoidable with Context, meaning we as developers cannot memoize, as well as the developers of this library cannot memoize, does that mean the only option left is to go old school with an event listener?

Thanks.

DesertStrom16 avatar Apr 13 '23 01:04 DesertStrom16

Can you give an example of this? Location has its own specific context, so it only updates when the location changes. Even if the pathname doesn't change, if you're triggering something that's causing another property of the location to change, that still will trigger a rerender (which is the correct behavior).

timdorr avatar Apr 13 '23 06:04 timdorr

This is a known bug with no official fix https://github.com/remix-run/react-router/issues/7634

process0 avatar Sep 23 '23 15:09 process0