Transition plan
Learn how Router-Lite handles the re-entrance of the same component and how to override the default behavior.
The transition plan in router-lite is meant for deciding how to process a navigation instruction that intends to load the same component that is currently loaded/active, but with different parameters. As the router-lite uses a sensible default based on the user-voice, probably you never need to touch this area. However, it is still good to know how to change those defaults, whenever you are in need to do that (and we all know that such needs will arise from time to time).
Transition plan can be configured using the transitionPlan
property in the routing configuration. The allowed values are replace
, invoke-lifecycles
, none
or a function that returns one of these values.
replace
: This instructs the router to completely remove the current component and create a new one, behaving as if the component is changed. This is the default behavior if the parameters are changed.invoke-lifecycles
: This instructs the router to call the lifecycle hooks (canUnload
,canLoad
,unloading
andloading
) of the component.none
: Does nothing. This is the default behavior, when nothing is changed.
How does it work
The child routes inherits the transitionPlan
from the parent.
When the transitionPlan
property in the routing configuration is not configured, router-lite uses replace
when the parameters are changed and none
otherwise.
It might be normal to think that the default selection of the replace
transition plan when the parameter changes, to be an overkill and the default selection should have been invoke-lifecycles
instead. As a matter of fact that's the default option in Aurelia1 as well as in earlier versions of Aurelia2. However, as understood from the user-voices that replace
would in this case cause less surprises. Hence the default behavior is changed to replace
.
Transition plans are inherited
Transition plans defined on the root are inherited by the children. The example below shows that the transitionPlan
on the root is configured to replace
and this transition plan is inherited by the child route configuration. This means that every time the link is clicked, the component is created new and the view reflects that as well.
See this example in action below.
Use a function to dynamically select transition plan
You can use a function to dynamically select transition plan based on route nodes. The following example shows that, where for every components, apart from the root component, invoke-lifecycles
transition plan is selected.
The behavior can be validated by alternatively clicking the links multiple times and observing that the CeOne#id2
increases, whereas CeOne#id1
remains constant. This shows that every attempt to load the CeOne
only invokes the lifecycle hooks without re-instantiating the component every time. You can try out this example below.
This can be interesting when dealing with sibling viewports, as you can select different transition plan for different siblings.
The example above selects invoke-lifecycles
for the CeTwo
and replace
for everything else. When you alternatively click the links multiple times, you can see that CeOne
is re-instantiated every time whereas for CeTwo
only the lifecycles hooks are invoked and the instance is reused. You can see the example in action below.
Last updated