【发布时间】:2015-11-06 02:34:43
【问题描述】:
我想要一条路线“/”:
- 如果经过身份验证 -> 将用户带到主页(以下示例中的 Pages.Feed)
- 如果未经过身份验证 -> 将用户带到 Landing(下例中为 Pages.Landing)
基本上,复制 facebook.com 或 twitter.com 的行为。
我想在路由级别做到这一点,而不是有一个构成逻辑的组件。
现在我有:
function requireAuth(nextState, replaceState) {
if (!store.getState().currentUser.nick) {
replaceState({ nextPathname: nextState.location.pathname }, '/landing');
}
}
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route component={Containers.App}>
<Route component={Containers.LandingLayout}>
<Route path="landing" component={Pages.Landing} />
<Route path="login" component={Pages.LogIn} />
<Route path="signup" component={Pages.SignUp} />
</Route>
<Route path="/" component={Containers.AppLayout}>
<IndexRoute component={Pages.Feed} onEnter={requireAuth}/>
<Route path="about" component={Pages.About} />
<Route path="help" component={Pages.Help} />
<Route path=":nick" component={Pages.Profile} />
<Route path=":nick/:slug" component={Pages.List} />
</Route>
</Route>
</Router>
</Provider>,
document.getElementById('listlogs')
);
如果经过身份验证,“/”会将用户带到他的主页 (Pages.Feed),否则会将用户重定向到“/landing”。在这里,我需要一条我想避免的着陆的特定路线。我希望它是“/”。
使用:
component={authLogic ? Pages.Feed : Pages.Landing}
不是解决方案。它最初会工作,即如果用户未登录,应用程序将加载登陆,如果用户登录,则加载提要页面。但是,由于 React 路由器应用程序已经呈现,到组件的映射不会一旦用户在前一种情况下登录或在后一种情况下退出,就可以工作。以某种方式重新渲染路由是否有意义?有没有更好的解决方案?
【问题讨论】:
标签: web-applications reactjs react-router