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

[Bug]: Modifying state triggers loader and parent loaders on route

Open hayatae opened this issue 3 years ago • 2 comments

What version of React Router are you using?

6.10.0

Steps to Reproduce

  1. Using the new createBrowserRouter, create any route with a loader on it.
  2. Inside the element for that route, create a link which navigates to the same route, but with state. This is useful for opening a dialog on a page. For example:
<Link to="." state={{ openModal: true }} replace={false}>Some Link</Link>
  1. Note that when the link is clicked, the loader for the existing page route as well as any loaders defined on any parent routes, are re-executed. All of the loaders are also re-executed when navigating back in the browser (route remaining the same, but state changing).

Expected Behavior

I would expect no loaders to execute when the route does not change but the state does.

Actual Behavior

All loaders in the tree are re-executing when route remains the same but state changes.

hayatae avatar Apr 23 '23 14:04 hayatae

Your loaders may depend on those state values. There is no way to know if this is true or not. So, running them again is the proper behavior.

timdorr avatar Apr 24 '23 02:04 timdorr

Aah, I see now that there is a shouldRevlaidate property which would solve this problem. If I may suggest, it would be helpful to mention related properties (such as shouldRevalidate) in the documentation for loader since it directly influences whether or not the loader is invoked and under what conditions. I just wasn't able to make that connection or understand that relation until reading your response here.

Thank you!

hayatae avatar Apr 24 '23 14:04 hayatae