React.js, wait for setState to finish before triggering a function?

前端 未结 5 718
庸人自扰
庸人自扰 2020-12-23 08:31

Here\'s my situation:

  • on this.handleFormSubmit() I am executing this.setState()
  • inside this.handleFormSubmit(), I am calling this.findRoutes(); - whic
5条回答
  •  庸人自扰
    2020-12-23 09:29

    setState takes new state and optional callback function which is called after the state has been updated.

    this.setState(
      {newState: 'whatever'},
      () => {/*do something after the state has been updated*/}
    )
    

提交回复
热议问题