gpt4 book ai didi

reactjs - 使用 react router v6 实现 protected 路由

转载 作者:行者123 更新时间:2023-12-05 00:55:26 26 4
gpt4 key购买 nike

已创建具有以下版本的 react js 仪表板应用程序“ react 路由器”:“^6.0.0-beta.0”,"react-router-dom": "^6.0.0-beta.0"

必须为应用程序实现一个 protected 路由实例,因为上述 protected 路由的 react 路由器 v6 与我在 v5 上习惯的有点不同。有人可以告诉我如何为此添加 protected 路由吗?感谢您的宝贵时间!

这是 app.js

import 'react-perfect-scrollbar/dist/css/styles.css';
import React from 'react';
import { useRoutes } from 'react-router-dom';
import { ThemeProvider } from '@material-ui/core';
import GlobalStyles from 'src/components/GlobalStyles';
import 'src/mixins/chartjs';
import theme from 'src/theme';
import routes from 'src/routes';

const App = () => {
const routing = useRoutes(routes);

return (
<ThemeProvider theme={theme}>
<GlobalStyles />
{routing}
</ThemeProvider>
);
};
export default App;

这里是 route.js 代码

const routes = [
{
path: 'app',
element: <DashboardLayout />,
children: [
{ path: 'account', element: <AccountView /> },
{ path: 'reporting', element: <CustomerListView /> },
{ path: 'dashboard', element: <DashboardView /> },
{ path: 'classrooms', element: <ProductListView /> },
{ path: 'settings', element: <SettingsView /> },
{ path: '*', element: <Navigate to="/404" /> }
]
},
{
path: '/',
element: <MainLayout />,
children: [
{ path: 'login', element: <LoginView /> },
{ path: 'register', element: <RegisterView /> },
{
path: 'RegisterViewContactDetails',
element: <RegisterViewContactDetails />
},
{ path: 'ForgotPassword', element: <ForgotPassword /> },
{ path: 'RestPassword', element: <RestPassword /> },
{ path: '404', element: <NotFoundView /> },
{ path: '/', element: <Navigate to="/login" /> },
{ path: '*', element: <Navigate to="/404" /> }
]
}
];
export default routes;

最佳答案

这是我使用 useRoutes Hook 实现 protected 路由的工作示例。

App.js

import routes from './routes';
import { useRoutes } from 'react-router-dom';

function App() {
const { isLoggedIn } = useSelector((state) => state.auth);

const routing = useRoutes(routes(isLoggedIn));

return (
<>
{routing}
</>
);
}

routes.js

import { Navigate,Outlet } from 'react-router-dom';

const routes = (isLoggedIn) => [
{
path: '/app', // protected routes
element: isLoggedIn ? <DashboardLayout /> : <Navigate to="/login" />,
children: [
{ path: '/dashboard', element: <Dashboard /> },
{ path: '/account', element: <Account /> },
{ path: '/', element: <Navigate to="/app/dashboard" /> },
{
path: 'member',
element: <Outlet />,
children: [
{ path: '/', element: <MemberGrid /> },
{ path: '/add', element: <AddMember /> },
],
},
],
},
{ // public routes
path: '/',
element: !isLoggedIn ? <MainLayout /> : <Navigate to="/app/dashboard" />,
children: [
{ path: 'login', element: <Login /> },
{ path: '/', element: <Navigate to="/login" /> },
],
},
];

export default routes;

关于reactjs - 使用 react router v6 实现 protected 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64626074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com