Using setInterval in React Component

前端 未结 3 1511
一生所求
一生所求 2020-12-06 13:18

I was reading the tutorial on the official react website. In the example about life cycle methods, under the componentDidMount method, a timerID is set to the setInterval fu

3条回答
  •  春和景丽
    2020-12-06 13:58

    It's Simple. As soon as it React executes componentDidMount() life cycle method, the timer starts running.

    this.timerID = setInterval(
          () => this.tick(),
          1000
        );
    

    The above timer will run until the component gets unmounted (according to your code). It's not a surprise that your code works that way.

提交回复
热议问题