When value is assigned to components state, why console.log prints the previous state?

后端 未结 5 1365
死守一世寂寞
死守一世寂寞 2020-11-30 09:26

I\'m sending values of numbers from Numbers component to Main component. Everything is working fine until I set that value in my Main component to that component\'s state.<

5条回答
  •  天涯浪人
    2020-11-30 09:28

    Probably because the console.log is triggered before the new state has been really set.

    You should use the function:

    componentDidUpdate: function() {
        console.log(this.state.number);
    }
    

    This function is triggered each time the state is updated.

    Hope it helps

提交回复
热议问题