Can I execute a function after setState is finished updating?

前端 未结 5 1742
温柔的废话
温柔的废话 2020-11-30 17:32

I am very new to ReactJS (as in, just started today). I don\'t quite understand how setState works. I am combining React and Easel JS to draw a grid based on

5条回答
  •  不知归路
    2020-11-30 17:59

    when new props or states being received (like you call setState here), React will invoked some functions, which are called componentWillUpdate and componentDidUpdate

    in your case, just simply add a componentDidUpdate function to call this.drawGrid()

    here is working code in JS Bin

    as I mentioned, in the code, componentDidUpdate will be invoked after this.setState(...)

    then componentDidUpdate inside is going to call this.drawGrid()

    read more about component Lifecycle in React https://facebook.github.io/react/docs/component-specs.html#updating-componentwillupdate

提交回复
热议问题