reactjs - 在reactjs中使用具有相同路由路径的条件组件

假设我有 2 个名为 <Dashboard/> 的组件在用户登录后呈现 <Landing/>当用户尚未登录时正在呈现 在reactjs中,如果我想使用相同的/怎么办?路由,如果用户尚未登录,它将呈现 <Landing/>它将呈现 <Dashboard/>如果用户已登录?

<Landing/>

<Route path="/" component={Landing} onEnter={appOnEnter}>
  <IndexRoute component={Home}/>

  ... Other not-login-required routes ...
</Route>

Dashboard

<Route path="/" component={Dashboard} onEnter={appOnEnter}>
  <IndexRoute component={Home} />

  ... Other login-required routes ...
</Route>

我遇到了 getComponent/getComponents我想我可以像这样使用

<Route path="/" getComponent={(nextState, cb) => {
 // do asynchronous stuff to find the components
 cb(null, Course)
}} />

但是this似乎不建议使用该方法

The reason we don't explicitly support this sort of pattern is because it's fairly atypical to wire things up this way

那么实现我想要的最好方法是什么?

最佳答案

您可以为该路由创建一个Home 组件,并且

return this.props.loggedIn ? <Dashboard /> : <Login />;

这样您就不必在路由级别处理它。

https://stackoverflow.com/questions/42474176/

相关文章:

spring-mvc - 如何在应用程序中使用 "RestController"和 "Control

list - 为什么这个删除 Haskell 中的重复函数有两个参数而不是一个?

oracle - Windows 的 ins-32025 oracle 11g 错误

bash - 在 bash 命令输出中添加前缀

c# - 无法将类型为 'System.DateTime' 的对象转换为类型 'System.Str

html - 将鼠标悬停在文本上时显示图像

date - 如何将天数添加到 Cypher 中的日期属性?

c# - 在 NSubstitute 中使用 IEnumerable 设置参数匹配器

xml - “||” (OR) 在XML中怎么写?

spring-boot - 将所有 Zuul 代理请求动态重新路由到单个 URL