【问题标题】:React, "this", cloneElement and es6React、“this”、cloneElement 和 es6
【发布时间】:2015-10-28 18:52:28
【问题描述】:

我想知道当你传递一个函数时 ES6 和 cloneElement 是如何工作的。我需要在父组件的状态中引用状态,但 this 引用子组件而不是父组件。

下面是用常规 JavaScript 编写的代码,使其工作,在第一次用 ES6 编写它并敲击键盘后,我决定看看它是否是 ES6,所以我重构了它,它工作得很好。

我只想用 ES6 编写它,因为其他一切都是这样,但这让我很难过。

这是我在 ES5 中的组件:

var Parent = React.createClass({
  content: function() {
    return React.Children.map(this.props.children, function(child) {
     return React.cloneElement(child, {
       passThisFunc: this.passThisFunc
     })
    }.bind(this));
  },

  passthisfunc: function(component) {
    // returns the components props
    console.log(this);

    // Returns the component so I can do component.props.name
    console.log(component);
  },

  render: function() {
    return (
      <div>
        { this.content }
      </div>
    )
  }
});

然后在它的孩子中:

var Child = React.createClass({
  componentDidMount: function() {
    this.props.passThisFunc(this);
  }

  render: function().....
});

组件在 ES6 中并没有太大的不同,它确实是在记录 this 时引用的。

任何重构方面的帮助(尤其是父组件)将不胜感激。

编辑 这是我尝试过的 ES6 示例:

class Parent extends React.Component {
  content() {
    return React.Children.map(this.props.children, function(child) {
     return React.cloneElement(child, {
       passThisFunc: this.passThisFunc
     })
    }.bind(this));
  }

  passthisfunc(component) {
    // returns the components props
    console.log(this);

    // Returns the component so I can do component.props.name
    console.log(component);
  }

  render() {
    return (
      <div>
        { this.content }
      </div>
    )
  }
};

class Child extends React.Component {
  componentDidMount() {
    this.props.passThisFunc(this);
  }

  render(){...}
};

【问题讨论】:

  • 你能修复content中的语法错误吗?
  • ES6 并没有改变 this 的工作方式。如果不是 child.props,您对 this 的期望值是多少?
  • 我在content 中的内容与我在当前实现中的内容基本相同,并且工作正常,语法错误是什么?同样关于你的第二个问题,我想做类似的事情:this.setState({ test: 'test' }) 所以我想我希望它是 this 以等于它在 ES5 中的父组件。
  • .map( 调用错过了它的右括号,并且在您传递给React.createClass({…}) 的那个对象文字中,第一个属性用; 而不是逗号分隔,第二个错过了完全逗号。
  • 好电话,谢谢。已更新。

标签: javascript reactjs ecmascript-6


【解决方案1】:

React.createClass 为 ES6 类提供了 was removedautobinding(另见 this article)。所以你现在必须手动完成:

…
  content: function() {
    return React.Children.map(this.props.children, function(child) {
     return React.cloneElement(child, {
       passThisFunc: this.passThisFunc.bind(this)
     })
    }.bind(this));
  },
…

但你不会在 ES6 中真正做到这一点。相反,您首先会使用箭头函数,它具有词法 this 绑定:

class Parent extends React.Component {
  constructor() {
    super();
    this.passthisfunc = (component) => {
      // returns the parent
      console.log(this);

      // Returns the component so I can do component.props.name
      console.log(component);
    };
  }
  content() {
    return React.Children.map(this.props.children, child =>
      React.cloneElement(child, {
        passThisFunc: this.passThisFunc
      });
    );
  }
  …
}

【讨论】:

  • 注意:箭头函数在每次渲染时重新创建函数 - 这是一个微性能命中,需要权衡更清晰的语法。
  • @AshleyCoolman 仅当在 render (或它调用的方法)中声明时,我还没有完成 afaik
猜你喜欢
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多