如果用户使用 React 路由器登录,如何从 /login 重定向到其他地方
How to redirect to from /login to elsewhere if user is signed-in using React router
我有这个:
<Router history={browserHistory}>
<Switch>
<Route path="/login" component={Login} />
</Switch>
</Router>
如果用户转到 /login 并且用户已经登录,我想将他们跳转到他们的设置页面。有没有办法用 Router/Route 以声明方式做到这一点,或者我是否需要在登录组件中调用类似的东西?
componentDidMount(){
if(this.props.user){
window.location.href = '/settings'
}
}
有什么方法可以做到这一点?
相应地使用<Redirect />
and set to
。
function AuthComponent(props) {
const { user } = props;
if (user.isAuthenticated) {
return (
<Redirect to={{
pathname: "/home",
state: {
from: location,
user // user props
}
}} />
);
}
return <Login />;
}
// PrivateRoute is an HOC injecting the user props.
<PrivateRoute exact path="/auth" component={AuthComponent} />
我有这个:
<Router history={browserHistory}>
<Switch>
<Route path="/login" component={Login} />
</Switch>
</Router>
如果用户转到 /login 并且用户已经登录,我想将他们跳转到他们的设置页面。有没有办法用 Router/Route 以声明方式做到这一点,或者我是否需要在登录组件中调用类似的东西?
componentDidMount(){
if(this.props.user){
window.location.href = '/settings'
}
}
有什么方法可以做到这一点?
相应地使用<Redirect />
and set to
。
function AuthComponent(props) {
const { user } = props;
if (user.isAuthenticated) {
return (
<Redirect to={{
pathname: "/home",
state: {
from: location,
user // user props
}
}} />
);
}
return <Login />;
}
// PrivateRoute is an HOC injecting the user props.
<PrivateRoute exact path="/auth" component={AuthComponent} />