基本上,我们在构造函数中绑定事件处理函数,或者在React类组件中将它们作为箭头函数,如下所示
class Test extends Component{
constructor(props){
super(props);
this.state = { count:0 };
this.setCount = this.setCount.bind(this);
}
setCount() {
this.setState({count: this.state.count + 1});
}
render() {
return <button onClick={this.setCount}>Increase</button>
}
}
但是在React v16中引入挂钩之后.7.0类组件成为具有状态的功能组件.
那么,我如何用函数组件中的钩子绑定函数呢?