【发布时间】:2018-11-13 10:02:33
【问题描述】:
我的要求是更新componentWillReceiveProps的map函数中的状态值。
在控制台日志中,我得到的只是 1,但 sub.subscribed 包含 0 和 1
控制台窗口引用:http://prntscr.com/jqifiz
constructor(props) {
super(props);
this.state = {
regionAll: [],
};
}
componentWillReceiveProps(nextProps){
if(nextProps.apiData !== false ){
nextProps.apiData.data.datacenter.category.map((sub)=> {
console.log(sub.subscribed,'sub.subscribed');
this.setState({
regionAll: [
...this.state.regionAll,
sub.subscribed
]
},()=>{
console.log(this.state.regionAll,'sub');
})
})
}
这是在 reactjs 中更新状态的正确方法吗?
【问题讨论】:
-
那么问题是什么
-
@Isaac 设置状态没有正确更新
-
您的控制台快照打印 ["1"] "sub"。但我可以看到 console.log(this.state.regionAll,'subbbb');这个说法 。可能是您在不同的位置进行检查。你的代码看起来不错
-
@stack26 抱歉,我更新了该代码并将 subbb 替换为 sub
-
控制台状态非常好
标签: reactjs react-redux react-state-management