我想要用于根据用户登录状态将用户重定向到登录屏幕或仪表板屏幕的代码。
我试图将操作放在componentWillMount()中,以检查它是否导致用户记录或没有结果。
然后我使用componentDidUpdate()捕获传入的道具,如下所示:
this.props.localUser.user_id > 0 && Actions.Dashboard();
this.props.localUser.user_id <= 0 && Actions.Login();
但是问题是,在reducer上,我的本地user_id值默认设置为0,因此在组件上将其挂载到该状态并尝试重定向到登录,然后在微秒内将其更新为user_id> 0,并且立即重定向到仪表板
看起来像屏幕重定向了两次
componentWillMount() {
setTimeout(() =>{
this.props.getLoggedInUserAction();
}, 3000);
}
componentDidUpdate() {
this.props.localUser.user_id < 0 && Actions.Login();
this.props.localUser.user_id > 0 && Actions.Dashboard();
}
我该如何解决这个问题/解决方案