question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Leave hook is not called when url Param changes in parameterized Route

See original GitHub issue

Hi, 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:open
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rjgamblecommented, Sep 16, 2021

I see. In such cases I usually question whether such logic needs to be in the routing layer. Is it possible to shift it in another place.

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.

1reaction
jackiems82commented, Sep 16, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found