【发布时间】:2018-05-31 11:03:30
【问题描述】:
基本上我是在理解别人的代码并进行修改。在 App.js 中检查用户是否登录,他必须呈现仪表板
App.js
<Switch>
<Redirect exact path="/" to="/dashboard"/>
<Route path="/login" component={GuestBoard} />
<EnsureSignedIn>
<Switch>
<Route path="/dashboard/" component={Dashboard}/>
<Route path="/welcome/" component={Welcome}/>
</Switch>
</EnsureSignedIn>
</Switch>
基本上<EnsureSignedIn>会检查用户是否登录,它会渲染所有的孩子。
我的问题是:<Switch> 如何渲染没有路径的<EnsureSignedIn>。 还有究竟发生了什么(渲染组件的流程是什么)如果我继续在 <Switch> 中编写 React 组件?
这样说
<Switch>
<Redirect exact path="/" to="/dashboard"/>
<Route path="/login" component={GuestBoard} />
<Component1 />
<Component2 />
<Component3 />
</Switch>
确保登录:
componentDidMount() {
if (!this.props.user) {
this.props.history.push('/login?next=' + this.props.currentURL);
}
render() {
return (this.props.user ? this.props.children : null);
}
我们使用了redux,所以user是reducer的props。
【问题讨论】:
-
主开关还能用吗?文档说子元素应该是 Route 或 Redirect 元素。 github.com/ReactTraining/react-router/blob/master/packages/…
-
是的。有用 !!!连我都感到惊讶。
-
如果这不是正确的实施方式,请告诉我。
-
好吧,如果它有效,那么我认为没关系。你能粘贴
EnsureSignedIn的代码吗? -
EnsureSignedIn:没有什么内容
标签: reactjs react-router react-router-dom dynamic-routing