ReactJS: setTimeout() not working?

前端 未结 10 2186

Having this code in mind:

var Component = React.createClass({

    getInitialState: function () {
        return {position: 0};    
    },

    componentDid         


        
10条回答
  •  Happy的楠姐
    2020-12-04 07:56

    I know this is a little old, but is important to notice that React recomends to clear the interval when the component unmounts: https://reactjs.org/docs/state-and-lifecycle.html

    So I like to add this answer to this discussion:

      componentDidMount() {
        this.timerID = setInterval(
          () => this.tick(),
          1000
        );
      }
      componentWillUnmount() {
        clearInterval(this.timerID);
      }
    

提交回复
热议问题