作者:翔未央图_971 | 来源:互联网 | 2022-12-08 10:26
1> I Putu Yoga ..:
箭头功能和法线功能不相等。
区别在于:
箭头函数没有自己的绑定功能this
,因此请this.setState
参考YourClass.setState
。
使用常规功能,您需要将其绑定到类以获得Class的this
引用。因此,当您this.setState
实际调用它时,它指的是YourFunction.setState()
。
样例代码
class FancyComponent extends Component {
handleChange(event) {
this.setState({ event }) // `this` is instance of handleChange
}
handleChange = (event) => {
this.setState({ event }) // `this` is instance of FancyComponent
}
}