I'm trying to familiarise myself with react router 6. Not a fan of it so far. I think this dynamic relative/absolute thing just makes it more complicated and vulnerable.
I'm upgrading from v5, and I was able to replace params without explicitly knowing the current path like:
const LinkReplaceItemId = ({ itemId }) => {
const { path } = useRouteMatch();
const params = useParams();
return (
<Link to={generatePath(path, { ...params, itemId })}>Link</Link>
)
}
There was no need to specify a pattern for the route match, it just matched whatever the current path is, and replaced the required params.
Now I have to specify the pattern to get the current path. What if I don't know the pattern - the component can be child of different route structures?
Consider the case where this can be used in:
- /items/:itemId
- /users/:userId/items/:itemId
- /collections/:itemId/properties
Just made up some examples to show an example where I want to keep the current url, just want to replace a specific piece.
How to do that in react router 6?