ReactJS: setTimeout() not working?

前端 未结 10 2178

Having this code in mind:

var Component = React.createClass({

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

    componentDid         


        
相关标签:
10条回答
  • 2020-12-04 07:54

    You didn't tell who called setTimeout

    Here how you call timeout without calling additional functions.

    1. You can do this without making additional functions.

    setTimeout(this.setState.bind(this, {position:1}), 3000);
    

    Uses function.prototype.bind()

    setTimeout takes the location of the function and keeps it in the context.

    2. Another way to do the same even by writing even less code.

    setTimeout(this.setState, 3000, {position:1});
    

    Probably uses the same bind method at some point

    The setTimeout only takes the location of the function and the function already has the context? Anyway, it works!

    NOTE: These work with any function you use in js.

    0 讨论(0)
  • 2020-12-04 07:54

    Your code scope (this) will be your window object, not your react component, and that is why setTimeout(this.setState({position: 1}), 3000) will crash this way.

    That comes from javascript not React, it is js closure


    So, in order to bind your current react component scope, do this:

    setTimeout(function(){this.setState({position: 1})}.bind(this), 3000);
    

    Or if your browser supports es6 or your projs has support to compile es6 to es5, try arrow function as well, as arrow func is to fix 'this' issue:

    setTimeout(()=>this.setState({position: 1}), 3000);
    
    0 讨论(0)
  • 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);
      }
    
    0 讨论(0)
  • 2020-12-04 07:58
    setTimeout(() => {
      this.setState({ position: 1 });
    }, 3000);
    

    The above would also work because the ES6 arrow function does not change the context of this.

    0 讨论(0)
  • 2020-12-04 07:59

    Try to use ES6 syntax of set timeout. Normal javascript setTimeout() won't work in react js

    setTimeout(
          () => this.setState({ position: 100 }), 
          5000
        );
    
    0 讨论(0)
  • 2020-12-04 08:00

    There's a 3 ways to access the scope inside of the 'setTimeout' function

    First,

    const self = this
    setTimeout(function() {
      self.setState({position:1})
    }, 3000)
    

    Second is to use ES6 arrow function, cause arrow function didn't have itself scope(this)

    setTimeout(()=> {
       this.setState({position:1})
    }, 3000)
    

    Third one is to bind the scope inside of the function

    setTimeout(function(){
       this.setState({position:1})
    }.bind(this), 3000)
    
    0 讨论(0)
提交回复
热议问题