码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-03-31 09:17:38      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:绑定   asc   .com   bind   创建   tcl   out   this   es6   

第一种:官方推荐的:

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三种方式

标签:绑定   asc   .com   bind   创建   tcl   out   this   es6   

原文地址:http://www.cnblogs.com/feiying100/p/6649611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!