当前位置:   jquery > 正文

在回调中使用this.setState

javascript,callback,this,reactjs,twitter,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

我有以下代码在react组件中获取twitter时间轴:

  componentWillMount: function() {
    twitter.get('statuses/user_timeline',
    function(error, data) {
      this.setState({tweets: data})
     });
  }

但我无法设置state那里,因为this没有设置回该函数中的组件.

如何在回调中设置状态?

nb console.log(data)而不是this.setState工作正常,这就是为什么我怀疑这个变量的问题.



1> Alexander T...:

您可以设置this.bind这样的方法,并呼吁twitter.getcomponentDidMount如本example

componentDidMount: function() {
   twitter.get('statuses/user_timeline', function(error, data) {
      this.setState({tweets: data})
   }.bind(this)); // set this that refers to you React component
}

Example

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/jquery/detail/13364
推荐阅读
相关标签
  

闽ICP备14008679号