[Bug]: navigate(to, { replace: true }) causes re-render and infinite loops #8592
Replies: 6 comments 1 reply
-
I am still new to the new |
Beta Was this translation helpful? Give feedback.
-
Have you ended up solving this? |
Beta Was this translation helpful? Give feedback.
-
I tried upgrading v5 to v6 today, but until this bug is fixed version 6 is hardly usuable... way too many rerenders due to this change. |
Beta Was this translation helpful? Give feedback.
-
the same issue v6.4.0 |
Beta Was this translation helpful? Give feedback.
-
same problem, not just query parameter, if you navigate to the same url, it still retenders |
Beta Was this translation helpful? Give feedback.
-
did you find a solution to this issue? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
What version of React Router are you using?
6.2.1
Steps to Reproduce
In a component rendered by a , I want to update the URL with search params based on component state to preserve values over refresh/other navigation.
IN INDEX.JS
IN COMPONENT
Expected Behavior
In previous versions of React Router, I have used
history.replace()
for this purpose, and that enables me to silently update the URL without causing a re-render and refetching data.Actual Behavior
As the
useEffect
code is written above, an infinite loop is created, as the useEffect is run afternavigate
is called (I assume this is because the new URL triggers a fresh mount of the component). A hacky fix is to check if my new query params in state are equal to the current url query params, and only callnavigate()
if they are not. However, this still causes my component to fetch new data from other useEffects each time the query params change, which is not necessary and is expensive.Beta Was this translation helpful? Give feedback.
All reactions