【发布时间】:2016-05-04 01:59:14
【问题描述】:
我有一个像下面这样的路由器:
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="login" component={Login}/>
</Route>
</Router>
这是我想要实现的目标:
- 如果未登录,将用户重定向到
/login - 如果用户在已经登录时尝试访问
/login,将他们重定向到root/
所以现在我正在尝试检查App 的componentDidMount 中的用户状态,然后执行以下操作:
if (!user.isLoggedIn) {
this.context.router.push('login')
} else if(currentRoute == 'login') {
this.context.router.push('/')
}
这里的问题是我找不到获取当前路由的 API。
我发现 this closed issue 建议使用 Router.ActiveState 混合和路由处理程序,但现在看来这两种解决方案已被弃用。
【问题讨论】:
标签: javascript reactjs react-router