preact-router
preact-router copied to clipboard
Add an option to use a new component instance when URL parameters change
It will be great if there is an option to force creating a new component instance when URL parameters change.
Currently we can use key attribute to let preact-router create a new component instance when navigating through different routes. For example, the key attribute in the example below prevents the A instance from being reused when navigating from /foo/1 to /foo/2.
<Router>
<A path="/foo/1" key="1" />
<A path="/foo/2" key="2" />
</Router>
What I am proposing is to introduce an option such that, for the same route (with parameter value change), old component instance won't be reused. For example, when I have <A path="/foo/:id" />, navigating from /foo/1 to /foo/2 should create a new instance of A.
What's your particular use case for that? Destroying a component and replacing it with the same one seems wasteful, but I may lack context you're having.
The component is wrapping a few non-preact components, which needs re-initialization upon certain props change.
Generally speaking, there are cases where creating another compoenent instance is faster. That's why preact support key attribute for components.
Of course, users (like me) can always wrap the component to achieve what I want, like:
const AWrap = ({ id }) => (<A id={id} key={id} />);
<Router>
<AWrap path="/foo/:id" />
</Router>
So I agree that supporting it in preact-router itself is not absolutely necessary.