【发布时间】:2020-04-18 17:24:48
【问题描述】:
我正在尝试实现仅登录的用户仪表板页面。
App.js
<BrowserRouter history={hist}>
<Route path="/sign-in" exact component={SignIn} />
<PrivateRoute path="/dashboard">
<Dashboard />
</PrivateRoute>
</BrowserRouter>
PrivateRoute.js
export function PrivateRoute({children, ...props}) {
return (
<Route {...props} render={( {location} ) =>
isAuthenticated ? children : <Redirect to="/sign-in" />
}
/>
);
}
const isAuthenticated = () => {
const userToken = JSON.parse(sessionStorage.getItem("user-token"));
return userToken ? true : false'
}
我正在检查 sessionStorage 中的用户令牌,如果它为 null,则返回 false 或 true。 事件虽然返回“false”,但它重定向到仪表板,而不是登录页面。 我的问题是什么?
【问题讨论】: