基本上我正在理解别人的代码并进行修改。在 App.js 中检查用户是否登录,他必须呈现仪表板
应用程序.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>
如果我继续在里面写 React 组件,到底会发生什么(组件的渲染流程是什么)<Switch>
?
说这样的话
<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。