29.React ajax中setState

$.ajax({
          url: url,
          type: "POST",
          timeout : 6000000, //超时时间设置,单位毫秒
          data: JSON.stringify(json),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success:function(d){
                    
            if(d.result==0){
                        
                 this.setState({
                  commission:d.data.commission
                 })
              }
                    
          }.bind(this)
    })

bind(this)

有时候在元素上绑定事件,像下面这样,看起来很正常,但是会报一些未定义的错误

<div onClick={this.hanldeClick}></div>

你可能需要这么操作

getInitialState: function() {
    this.handleClick = this.handle.bind(this)
    return {};
},    
constructor(props){
  super(props)
  this.handleClick = this.handleClick.bind(this)     
}
原文地址:https://www.cnblogs.com/famLiu/p/7284382.html