简体   繁体   English

React,“this”,cloneElement和es6

[英]React, “this”, cloneElement and es6

I am wondering how ES6 and cloneElement works when you pass it a function. 我想知道当你传递一个函数时ES6和cloneElement是如何工作的。 I need to reference state in the parent component's state but this references the child component and not the parent. 我需要在父组件的状态引用状态,但this引用的子组件,而不是父母。

Below is the code in regular JavaScript to make it work, after first writing it in ES6 and banging my head on the keyboard I decided to see if it was ES6 so I refactored and it works just fine. 下面是常规JavaScript中的代码使它工作,在第一次在ES6中编写它并在键盘上敲我的头后我决定看看它是否是ES6所以我重构并且它工​​作得很好。

I just want to write it in ES6 because everything else is but this has stumped me. 我只是想在ES6中写它,因为其他一切都是,但这让我很难过。

This is my component in ES5: 这是我在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>
    )
  }
});

And then in its children: 然后在它的孩子们:

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

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

The components are not that different in ES6, it is really what is referenced when this is logged. 该部件不在ES6不同,它是当真的是被引用this记录。

Any help in refactoring (especially the parent component) would be greatly appreciated. 任何重构帮助(特别是父组件)将不胜感激。

Edit Here is the ES6 Example I tried: 编辑这是我试过的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(){...}
};

The autobinding that React.createClass did feature was removed for ES6 classes (see also this article ). 为ES6类删除React.createClass确实功能的自动 React.createClass (另请参阅本文 )。 So you'll have to do it manually now: 所以你现在必须手动完成:

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

But you wouldn't really do this in ES6. 但你不会在ES6中真正做到这一点。 Rather, you'd use an arrow function in the first place, which features a lexical this binding: 相反,你首先使用一个箭头函数,它具有词法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
      });
    );
  }
  …
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM