【发布时间】:2018-10-25 01:05:58
【问题描述】:
我们都知道我们需要在 React 中绑定函数才能使其工作。我知道为什么我们需要绑定它。
但我不知道为什么我们不需要绑定箭头函数。
示例: 使用箭头函数(不需要绑定)
handleClick = () => {
this.setState({
isToggleOn: !this.state.isToggleOn
});
};
现在,使用函数(需要绑定)
this.handleClick = this.handleClick.bind(this);
handleClick() {
this.setState({
isToggleOn: !this.state.isToggleOn
});
};
我不是在问为什么我们需要在函数中绑定。我只想知道为什么箭头函数不需要绑定。
谢谢。
【问题讨论】: