理解React中es6方法建立组件的this

首发于:https://mingjiezhang.github.io/(转载请说明此出处)。javascript

在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的。java

从react中的demo提及

Facebook最近一次更新react时,将es6中的class加入了组件的建立方式当中。Facebook也推荐组件建立使用经过定义一个继承自 React.Component 的class来定义一个组件类。官方的demo:react

class LikeButton extends React.Component {
  constructor() {
    super();
    this.state = {
      liked: false
    };

    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    this.setState({liked: !this.state.liked});
  }
  render() {
    const text = this.state.liked ? 'liked' : 'haven\'t liked';
    return (
      <div onClick={this.handleClick}>
        You {text} this. Click to toggle.
      </div>
    );
  }
}
ReactDOM.render(
  <LikeButton />,
  document.getElementById('example')
);

上面的demo中有大量this的使用,在 class LikeButton extends React.Component 中咱们是声明该class,由于this具体是由其上下文决定的,所以在类定义中咱们没法得知this用法。 至关因而new了上面定义的类,首先调用 constructor() 函数, this.state 的this上下文就是该实例对象;同理,render() 函数中 this.state.liked 的this上下文也是该对象。问题在于 onClick={this.handleClick} ,获取该函数引用是没有问题,这里的this上下文就是该对象。git

这时问题来了,在原来 React.createClass 中, handleClick() 在onClick事件触发的时候,会自动绑定到LikeButton实例上,这时候该函数的this的上下文就是该实例。不过在ES6的class的写法中,Facebook取消了自动绑定,实例化LikeButton后,handleClick()的上下文是div的支撑实例( backing instance ),而 handleClick() 本来要绑定的上下文是LikeButton的实例。对于该问题,咱们有多种解决方案。es6

使用bind()函数改变this的上下文

能够在class声明中的constructor()函数中,使用github

this.handleClick = this.handleClick.bind(this);

该方法是一个bind()绑定,屡次使用。在该方法中,咱们在声明该实例后,能够在该实例任何地方使用 handleClick() 函数,而且该 handleClick() 函数的this的上下文都是LikeButton实例对象。编程

除此咱们也能够在具体使用该函数的地方绑定this的上下文到LikeButton实例对象,代码以下函数

<div onClick={this.handleClick.bind(this)}>
  You {text} this. Click to toggle.
</div>

这种方法须要咱们每次使用bind()函数绑定到组件对象上。this

es6的箭头函数

es6中新加入了箭头函数=>,箭头函数除了方便以外还有而一个特征就是将函数的this绑定到其定义时所在的上下文。这个特征也能够帮助咱们解决这个问题。使用以下代码:code

<div onClick={() => this.handleClick()}>
  You {text} this. Click to toggle.
</div>

这样该 this.handleClick() 的上下文就会被绑定到LikeButton的实例对象上。我的认为,使用箭头函数使得JavaScript更加接近面向对象的编程风格。

this的总结

this的本质就是:this跟做用域无关的,只跟执行上下文有关。

欢迎指正交流。

相关文章
相关标签/搜索