Ruslan Ermahgerd
Ruslan Ermahgerd

Reputation: 455

React Router 6 unmount component on route change

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

Answers (1)

gokulsgr
gokulsgr

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

Related Questions