Home > OS >  React Router 6 unmount component on route change
React Router 6 unmount component on route change

Time:01-27

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

CodePudding user response:

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} />
  }
];
  •  Tags:  
  • Related