In react-router V6, can you still use a parameter as React key?
In react-router V5, this could be achieved by writing:
<Switch>
<Route path="products/:productId" render={({match}) => <ProductDetail key={match.params.productId}/>} />
</Switch>
Now, after the switch to react-router V6, how do you pass the key?
<Routes>
<Route path="products/:productId" element={<ProductDetail key={?}/>}/>
</Routes>
Or is that no longer possible and should you add a wrapper container, which receives the productId param from useParams()
and passes it on?
MyWrapper = () => {
const { productId} = useParams();
return <ProductDetail key={productId}/>;
}
2
Answers
You can create a HOD (Higher order component) which takes component as a prop, that would eliminate creating wrappers each time.
Wrapper
Route