【发布时间】:2021-12-20 03:40:49
【问题描述】:
我正在使用 React Router v6 并为我的应用程序创建私有路由。
在文件 PrivateRoute.js 中,我有代码
import React from 'react';
import {Route,Navigate} from "react-router-dom";
import {isauth} from 'auth'
function PrivateRoute({ element, path }) {
const authed = isauth() // isauth() returns true or false based on localStorage
const ele = authed === true ? element : <Navigate to="/Home" />;
return <Route path={path} element={ele} />;
}
export default PrivateRoute
在文件 route.js 我写成:
...
<PrivateRoute exact path="/" element={<Dashboard/>}/>
<Route exact path="/home" element={<Home/>}/>
我也经历过同样的例子React-router Auth Example - StackBlitz, file App.tsx
我有什么遗漏吗?
【问题讨论】:
标签: javascript reactjs react-router-dom