React: 'Redirect' is not exported from 'react-router-dom'
Solution 1:
For react-router-dom
v6, simply replace Redirect
with Navigate
import { Navigate } from 'react-router-dom';
.
.
.
{ component: () => <Navigate to="/404" /> }
Solution 2:
Redirect has been removed from v6. You can replace Redirect with Navigate.
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';
import Home from '../home/Home';
export default function App() {
return (
<Router>
<Routes>
<Route path="/home" element={<Home />} />
<Route path="/" element={<Navigate replace to="/home" />} />
</Routes>
</Router>
);
}
Solution 3:
Redirect
component has been removed from the react-router
version 6.
From react router docs:
The
<Redirect>
element from v5 is no longer supported as part of your route config (inside a ). This is due to upcoming changes in React that make it unsafe to alter the state of the router during the initial render. If you need to redirect immediately, you can either a) do it on your server (probably the best solution) or b) render a<Navigate>
element in your route component. However, recognize that the navigation will happen in auseEffect
.
If you want to use Redirect
component, you will have to use react router version 5.
Alternatively, you can use Navigate component from react router v6. A <Navigate>
element changes the current location when it is rendered
import { Navigate } from "react-router-dom";
return (
<Navigate to="/dashboard" replace={true} />
)
Note: Navigate
is a component wrapper around useNavigate hook. You can use this hook to change routes programmatically.