Leave hook is not called when url Param changes in parameterized Route
See original GitHub issueHi, I have a problem that the leave hook seems not to be called for routes with parameters when navigating to the “same” route but with different parameter.
router.on(
'/foo/:id',
({ data }) => console.log('Routing to', data.id),
{
leave: () => console.log('Executing leave hook')
}
);
router.navigate('/foo/123')
router.navigate('/foo/321') // leave hook is not called
router.navigate('/home') // leave hook is called
The same actually happens for query parameters. I would’ve expected that any navigate call also calls the hooks.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
React Router does not update component if url parameter ...
When a URL's path changes, the current Component is unmounted and the new component pointed by the new URL is mounted. However, when...
Read more >The Complete Guide to URL parameters with React Router
As of v5.1, React Router comes with a useParams Hook that returns an object with a mapping between the URL parameter and its...
Read more >setRouteLeaveHook equivalent for url parameters change ...
I would like to have something like setRouteLeaveHook which would prevent transition for url parameters change.
Read more >Using Hooks with React Router
In this tutorial, you can learn how to use Hooks with React Router ... a User page component that accepts an id as...
Read more >React Router Hooks: Exploring How They Work
This command creates a new project called react-router-tutorial . ... Next, we have to change the Shop route to accept a URL parameter....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Its a good question. Unfortunately in our case, the routing layer is the only option we have for detecting that navigation event and performing that disposal and cleanup. Not to mention, execution in the hooks already provides us with all the context we need to perform our cleanup.
Thank you for the feedback @krasimir. We added some guard handling on top of the leave hook to prevent losing unsaved changes. All of this works great on non-parameterized routes. But for a page where you can display editable documents in tabs it would not work. If you say it should be implemented outside of the general routing logic I need to see how I could implement that in our wrapping logic. In general I would still consider that as a route change. At least the vue-router does it the same way. Maybe others as well?