ReactJS this.setState() out of sync with this.state.myvar

后端 未结 1 993
轮回少年
轮回少年 2020-12-15 10:16

In this ReactJS code in component I would expect this.setState( {b_MyPartyCat: value} ) to update this.state.b_MyPartyCat so the two console.log s

相关标签:
1条回答
  • 2020-12-15 10:51

    setState actually queues a state update. If you want to do something after it's actually executed, you can pass a callback as the second argument.

    updatePartyCategory: function(value) {
        this.setState({b_MyPartyCat: value}, function(){
            console.log(this.state.value === value); // true
        }.bind(this));
    },
    
    0 讨论(0)
提交回复
热议问题