search

Home  >  Q&A  >  body text

javascript - react state change problem

I change the state value in react. First, the view has been rendered, but after changing the value, the display is still the original value

Thanks

天蓬老师天蓬老师2751 days ago438

reply all(3)I'll reply

  • 为情所困

    为情所困2017-05-19 10:23:40

    http://stackoverflow.com/a/36...

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:23:40

    React has its life cycle, this.state will change in the next cycle, not this cycle.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:23:40

    setState() is asynchronous!

    If you want to see the updated state, you have to use callback:

    setState(new_state, () => {
       console.log(this.state);
    });

    Also, if multiple setState() are called together, it may accumulate to refresh the page once.

    reply
    0
  • Cancelreply