You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi guys,
I'm having troubles dealing with the lifecycle of the router, let me explain.
Since the order reflects what actually happens in the DOM, the leaving parts comes after the entering ones.
This is giving me troubles in the sense that, suppose I'm in /users/:id:
I want to populate some global state when I enter the route and clear it when I'm leaving.
onAfterEnter is basically required because that's when I'm sure the component is loaded.
I cannot use onAfterLeave because it's the last one, so null would override the actual id on same-route navigation. So I have to use onBeforeLeave.
I have to write the same code in onBeforeEnter because otherwise, if I navigate to the same route (even with the same param), onBeforeLeave runs and sets my state to null.
This example works, but it's setting my state twice on each navigation and it needs duplicated code.
Is the problem solvable in any other way? This honestly seems counterintuitive, and even if that's the actual order of the DOM operations it performs, I think it'd be easier for devs if the order was something like this:
onBeforeEnter
onAfterEnter
onBeforeLeave
onAfterLeave
Which I think is what we see with other famous frameworks and libraries. What do you think? :)
The text was updated successfully, but these errors were encountered:
Hi guys,
I'm having troubles dealing with the lifecycle of the router, let me explain.
Since the order reflects what actually happens in the DOM, the leaving parts comes after the entering ones.
This is giving me troubles in the sense that, suppose I'm in
/users/:id
:I want to populate some global state when I enter the route and clear it when I'm leaving.
Right now I'm stuck with this code:
onAfterEnter
is basically required because that's when I'm sure the component is loaded.onAfterLeave
because it's the last one, sonull
would override the actual id on same-route navigation. So I have to useonBeforeLeave
.onBeforeEnter
because otherwise, if I navigate to the same route (even with the same param),onBeforeLeave
runs and sets my state to null.This example works, but it's setting my state twice on each navigation and it needs duplicated code.
Is the problem solvable in any other way? This honestly seems counterintuitive, and even if that's the actual order of the DOM operations it performs, I think it'd be easier for devs if the order was something like this:
onBeforeEnter
onAfterEnter
onBeforeLeave
onAfterLeave
Which I think is what we see with other famous frameworks and libraries. What do you think? :)
The text was updated successfully, but these errors were encountered: