我有一个无法解决的反应路由问题。除了登录页面外,我已经认证了到所有其他设备的路由。身份验证工作正常,单击按钮和链接时可以浏览页面,但是当直接在URL中输入路由时,我将重定向到localhost:3000 /。我知道为什么会这样,我只是想不出一个解决办法。以下是所有相关代码。主要问题是登录组件中的useEffect挂钩重定向到/。
当我在url中输入类似localhost:3000 / user之类的内容时,我将被发送到localhost:3000 /
我该怎么办?
PrivateRoute.js
const PrivateRoute = ({ component: Component, ...rest }) => {
const authContext = useContext(AuthContext);
const { isAuthenticated } = authContext;
return (
<Route
{...rest}
render={props =>
!isAuthenticated ? <Redirect to='/login' /> : <Component {...props} />
}
/>
);
};
Login.js
const Login = props => {
useEffect(() => {
if (isAuthenticated) {
props.history.push('/');
}
}, [error, onLoad, isAuthenticated, props.history]);
useEffect(() => {
onLoad();
}, [error, onLoad, isAuthenticated, props.history]);
}