React 事件绑定this指向

1. 推荐:使用class的实例方法

class Hello extends React.Component {
    handleClick = () => {
        this.setState({ ... })
    }
}

2. 箭头函数

<button onClick={() => { this.handleClick() }} />

3. bind

constructor () {
  super()
  this.handleClick = this.handleClick.bind(this)    
}

 

posted @ 2021-08-14 20:56  键盘敲飞  阅读(21)  评论(0编辑  收藏  举报