document is not defined when attempting to setState from the return of an async call in componentWillMount

后端 未结 3 2507
余生分开走
余生分开走 2021-02-20 04:02

I grab my data in my componentWillMount call of my component [actually it\'s in a mixin, but same idea]. After the ajax call returns, I attempt to setState, but I get the error

3条回答
  •  星月不相逢
    2021-02-20 04:40

    Trying this out in a simple component, the following works just fine:

      getInitialState: function() {
        return {
          title: 'One'
        };
      },
    
      componentWillMount: function() {
        setTimeout(function(){
          this.setState({
            title: 'Two'
          });
        }.bind(this), 2000);
      },
    

    Can you post the exact error, and perhaps the stacktrace, so we may better see the problem you are having?

提交回复
热议问题