[英]I need you to redirect to Shop if url is wrong. React js
I need it to redirect to my main page if the url is wrong, the problem must be in this part, because before I used Redirect, but in the new version of React it is Navigate and I don't know if the way of entering changed如果url错误,我需要它重定向到我的主页,问题肯定出在这部分,因为我之前使用Redirect,但在新版本的React中是Navigate,我不知道进入方式是否正确变了
import React from 'react';
import {Routes, Route, Navigate} from 'react-router-dom';
import {authRoutes, publicRoutes} from '../routes';
import { SHOP_ROUTE } from '../utils/consts';
const AppRouter = () => {
const isAuth = false
return (
<Routes>
{isAuth && authRoutes.map(({path, Component}) =>
<Route key={path} path={path} element={<Component/>} exact/>
)}
{publicRoutes.map(({path, Component}) =>
<Route key={path} path={path} element={<Component/>} exact/>
)}
<Navigate to={SHOP_ROUTE}/>
</Routes>
);
};
export default AppRouter;
If you include a <Route path="*" />
component (as shown in the docs ) at the end of the <Routes />
, then the last route will be shown when no other route has been matched.如果您在
<Routes />
的末尾包含一个<Route path="*" />
组件(如文档中所示),那么当没有其他路由匹配时,将显示最后一条路由。
import React from 'react';
import {Routes, Route, Navigate} from 'react-router-dom';
import {authRoutes, publicRoutes} from '../routes';
import { SHOP_ROUTE } from '../utils/consts';
const AppRouter = () => {
const isAuth = false
return (
<Routes>
{isAuth && authRoutes.map(({path, Component}) =>
<Route key={path} path={path} element={<Component/>} exact/>
)}
{publicRoutes.map(({path, Component}) =>
<Route key={path} path={path} element={<Component/>} exact/>
)}
<Route path="*" element={<NotFound />} />
</Routes>
);
};
export default AppRouter;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.