I wanted to use 3 things from react-router-dom to implement router in my application i.e. Routes, useNavigate
and Redirect
. But the problem is Routes and useNavigate
is not available in react-router-dom 5.2.0. So, i installed react-router-dom v6.0.0-alpha.1 in order to use Routes and useNavigate
. But the problem is Redirect is removed from react-router-dom v6.0.0-alpha.1. Now i don't understand how i am going to use Routes, useNavigate
and Redirect
under specific react-router-dom
import React, { lazy, Suspense, useEffect } from "react";
import { Routes, Route, useNavigate, useLocation, Redirect } from "react-router-dom";
import { Loader } from "../common";
import HTML5Backend from "react-dnd-html5-backend";
import { DragDropContext } from "react-dnd";
const StudentList = lazy(() => import("../pages/StudentList"));
const About = lazy(() => import("../pages/About"));
const App = () => {
const navigate = useNavigate();
const currentModule = "/" + useLocation().pathname.split("/")[1];
const onLoad = () => {
navigate(currentModule);
};
useEffect(onLoad, []);
return (
<>
<Suspense fallback={<Loader />}>
<Routes>
{[
<Route
key="/student"
path="/studentlist/*"
element={<StudentList />}
/>,
<Route key="/about" path="/about/*" element={<About />} />,
<Redirect key="redirect" from="/" to="/studentlist" />,
]}
</Routes>
</Suspense>
</>
);
};
App.displayName = "App";
export default DragDropContext(HTML5Backend)(App);
question from:
https://stackoverflow.com/questions/65902269/how-to-use-routes-and-redirect-from-same-npm-package-react-router-dom 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…