es6编写reactjs事件处理函数绑定this三种方式

Posted 飞哥100

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es6编写reactjs事件处理函数绑定this三种方式相关的知识,希望对你有一定的参考价值。

第一种:官方推荐的:

class LoginControl extends React.Component {
  constructor(props) {
    super(props);
    this.handleLoginClick = this.handleLoginClick.bind(this);
    this.handleLogoutClick = this.handleLogoutClick.bind(this);
    this.state = {isLoggedIn: false};
  }

  

第二种:比较方便

render() {
    return (
      <div onClick={this.handleClick.bind(this)}>ES6方式创建的组件</div>
    );
  }

  

第三种:箭头函数

return (
      <button onClick={(e) => this.handleClick(e)}>
        Click me
      </button>
    );

  

以上是关于es6编写reactjs事件处理函数绑定this三种方式的主要内容,如果未能解决你的问题,请参考以下文章