React与ES6(三)

前端之家收集整理的这篇文章主要介绍了React与ES6(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果你看到前篇CartItemrender方法里的这一句:{this.increaseQty.bind(this)}你可能会很意外为什么要bind

如果把bind删掉的话会怎么样呢?会报错Uncaught TypeError: Cannot read property ‘setState’ of undeined

这是因为this.increaseQtythis指向的不是类,而是undefined。ES6的类不支持自动绑定,不能像ES5那样。比如React.createClass()这样全部的内部方法都是自动绑定好了。

下面我们就看看ES6写的React组件类如何绑定内部方法

方法一、使用Function.prototype.bind()

  1. class CartItem extends React.Component {
  2.  
  3. render() {
  4. return (
  5. <p className="large-4 column"> <button onClick={this.increaseQty.bind(this)} className="button success">+</button> <button onClick={this.decreaseQty.bind(this)} className="button alert">-</button> </p> ); } }

ES6的类本质就是Javascript方法,从Function的prototype里继承了bind()方法。所以当我们调用increaseQty()方法的时候,this指向了类的实例。你可以在MDN文档里看到更多

方法二、使用在constructor里定义的方法

这是方法一和constructor的混合使用:

  1. export default class CartItem extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.increaseQty = this.increaseQty.bind(this);
  5. }
  6.  
  7. render() {
  8. <button onClick={this.increaseQty} className="button success">+</button> } }

这个方法就是把bind()方法从JSX定义里换到了类的constructor里。

方法三、使用箭头方法和构造函数

ES6的胖箭头方法调用的时候保留了this指向的上下文。我们可以使用这一特性在构造函数里重新定义increaseQty()方法

  1. export default class CartItem extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. // this.state = {
  5. // qty: props.initialQty,
  6. // total: 0
  7. // };
  8. this._increaseQty = () => this.increaseQty();
  9. }
  10.  
  11. render() {
  12. <button onClick={_this.increaseQty} className="button success">+</button> } }

方法四、使用胖箭头方法和ES2015的class property

  1. export default class CartItem extends React.Component {
  2.  
  3. increaseQty = () => this.increaseQty();
  4.  
  5. render() {
  6. <button onClick={this.increaseQty} className="button success">+</button> }

因此,类属性非常的简洁。

注意:类属性不是当前Javascript的标准,但是Babel已经支持这个功能(stage 0)。你可以看这里了解更多。

方法五、使用ES2015的方法绑定语法

Babel又加入了对一个语法糖的支持Function.prototype.bind()::。这里就不介绍这个操作符是怎么工作的了。

  1. export default class CartItem extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.increaseQty = ::this.increaseQty;
  5. }
  6.  
  7. render() {
  8. <button onClick={this.increaseQty} className="button success">+</button> } }

最后

老外介绍的几个方法有点地方差别都不大,有的甚至只是调用的地方从constructor换到了JSX里。所以,有些就略去了,其他的可以看看。

猜你在找的React相关文章