Javascript React his.setState不是一个函数

Javascript React his.setState不是一个函数,javascript,node.js,function,reactjs,state,Javascript,Node.js,Function,Reactjs,State,这是父元素: changedData(){ this.setState({ changed: true }).bind(this) } 我把它传给孩子: <AboutMe changedData={this.changedData} auth={this.props.auth} profile={profile}/> 在函数中调用它之前,请确保绑定到它。 您可以将changedData函数签名更改为ES6 arrow函数,如下所示,以自动

这是父元素:

  changedData(){
    this.setState({
      changed: true
    }).bind(this)
  }
我把它传给孩子:

<AboutMe  changedData={this.changedData} auth={this.props.auth} profile={profile}/>  
在函数中调用它之前,请确保绑定到它。 您可以将changedData函数签名更改为ES6 arrow函数,如下所示,以自动绑定它:

changedData = () => {
}
或者可以在子组件中绑定它,如下所示:

<AboutMe 
    changedData={this.changedData.bind(this)} 
    auth={this.props.auth} 
    profile={profile}/>  

或者你可以在构造函数中绑定它,这比第二个建议更可取,因为内联绑定会在每个渲染上创建一个新的绑定函数。我对reactjs没有太多的工作,所以在需要的时候我只使用箭头函数进行绑定。与构造函数@RobM中的绑定相比,将其用于绑定是否有任何缺点?AFAIK arrow方法声明只是构造函数中绑定的语法糖,不具有内联绑定的缺点
<AboutMe 
    changedData={this.changedData.bind(this)} 
    auth={this.props.auth} 
    profile={profile}/>