1
需要加载我的主要组件,并且如果使用react-router将对值“logged:true”的本地存储重定向到“/ app”。componentWidMount中的Redux状态更改在componentDidMount中无法识别?
我使用的反应,终极版,这是我的代码:
class Main extends Component {
componentWillMount(){
// Return true in redux state if localstorage is found
this.props.checkLogStatus();
}
componentDidMount(){
// redirect in case redux state returns logged = true
if(this.props.logStatus.logged){
hashHistory.push('/app');
}
}
render() {
return (
<App centered={true} className="_main">
{this.props.children}
</App>
);
}
}
我的终极版动作:
checkLogStatus() {
// check if user is logged and set it to state
return {
type: LOGIN_STATUS,
payload: window.localStorage.sugarlockLogged === "true"
};
}
但是,当组件获得的componentDidMount阶段,我的终极版状态到现在还没有已更新。
Ÿ设法得到这种利用工作:
componentWillReceiveProps(nextProps){
if (nextProps.logStatus.logged && nextProps.logStatus.logged !== this.props.logStatus.logged){
hashHistory.push('/app');
}
}
但我不知道这是最好的解决方法。
提前致谢!