【发布时间】:2017-02-20 13:55:15
【问题描述】:
我有一个登录页面,我在其中使用componentWillReceiveProps 路由到下一页。但是我在componentWillReceiveProps 中设置的state 似乎没有设置。
这是我的componentWillReceiveProps 方法:
componentWillReceiveProps(nextProps) {
if (nextProps.isAuthenticated === true) {
browserHistory.push('/home');
} else {
console.log("this.props :::" + JSON.stringify(this.props))
console.log("this.state :::" + JSON.stringify(this.state))
console.log("nextProps :::" + JSON.stringify(nextProps))
this.setState({
errorMessage: nextProps.authenticationError
})
console.log("this.state :::" + JSON.stringify(this.state))
}
}
我得到的console output 是这样的:
this.props :::{"authenticationError":null}
this.state :::{"username":"35135","password":"3135","errorMessage":""}
nextProps :::{"isAuthenticated":false,"authenticationError":"Could not find user in DB."}
this.state :::{"username":"35135","password":"3135","errorMessage":""}
这里即使设置了状态,我的状态也没有改变。
请告诉我我做错了什么。
编辑:我有这个组件是 ErrorText ,它接受 errroMessage 属性。
<ErrorText errorMsg={this.state.errorMessage}></ErrorText>
【问题讨论】:
标签: reactjs