Reputation: 455
I use the same component on different react router routes. Does anyone know how to unmount component on route change? Now when I change the route, component doesn't unmount, here is my example: https://codesandbox.io/s/stupefied-cloud-bxfyf?file=/src/MyComponent.js
Upvotes: 1
Views: 1501
Reputation: 166
We can pass key to the componenet to uniquely identify the Component
const routes = [
{
path: "/",
element: <MyComponent key={1} />
},
{
path: "two",
element: <MyComponent key={2} />
}
];
Upvotes: 6