gpt4 book ai didi

javascript - React this.setState 不是一个函数

转载 作者:行者123 更新时间:2023-12-03 04:36:13 25 4
gpt4 key购买 nike

这是父元素:

  changedData(){
this.setState({
changed: true
}).bind(this)
}

我将其传递给 child :

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

child :

  dataChanged(data) {
this.props.changedData();
console.log("dataChanged!!")

}

最佳答案

确保在函数内调用 this 之前绑定(bind)它。您可以将 changedData 函数签名更改为 ES6 箭头函数,如下所示以自动绑定(bind)它:

changedData = () => {
}

或者您可以将其绑定(bind)在子组件中,如下所示:

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

关于javascript - React this.setState 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43289126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com