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

后端 未结 5 1371
死守一世寂寞
死守一世寂寞 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:48

    The setState method is asynchronous, so the new state is not there yet on console.log call. You can pass a callback as a second parameter to setState and call console.log there. In this case the value will be correct.

提交回复
热议问题