[Bug]: Modifying state triggers loader and parent loaders on route
What version of React Router are you using?
6.10.0
Steps to Reproduce
- Using the new
createBrowserRouter, create any route with a loader on it. - 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>
- 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.
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.
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!