admin 管理员组文章数量: 1184232
I am wondering how ES6 and cloneElement works when you pass it a function. I need to reference state in the parent component's state but this references the child component and not the parent.
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.
I just want to write it in ES6 because everything else is but this has stumped me.
This is my component in 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.
Any help in refactoring (especially the parent component) would be greatly appreciated.
Edit Here is the ES6 Example I tried:
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(){...}
};
I am wondering how ES6 and cloneElement works when you pass it a function. I need to reference state in the parent component's state but this references the child component and not the parent.
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.
I just want to write it in ES6 because everything else is but this has stumped me.
This is my component in 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.
Any help in refactoring (especially the parent component) would be greatly appreciated.
Edit Here is the ES6 Example I tried:
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(){...}
};
Share
Improve this question
edited Aug 5, 2015 at 20:51
Riley Bracken
asked Aug 5, 2015 at 20:10
Riley BrackenRiley Bracken
6,3792 gold badges18 silver badges17 bronze badges
13
|
Show 8 more comments
1 Answer
Reset to default 17The autobinding that React.createClass did feature was removed for ES6 classes (see also this article). 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. Rather, you'd use an arrow function in the first place, which features a lexical this binding:
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
});
);
}
…
}
本文标签: javascriptReact quotthisquot cloneElement and es6Stack Overflow
版权声明:本文标题:javascript - React, "this", cloneElement and es6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1739467982a2059020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
content? – Bergi Commented Aug 5, 2015 at 20:19thisworks. What value did you expect forthisif notchild.props? – Bergi Commented Aug 5, 2015 at 20:21contentis essentially the same as what I have in my current implementation and it works just fine, what are the syntax errors? Also on your second question I want to do something like:this.setState({ test: 'test' })So I guess I expect it to bethisto equal the parent component in like it does in ES5. – Riley Bracken Commented Aug 5, 2015 at 20:32.map(call misses its closing parenthesis, and in that object literal you're passing toReact.createClass({…})the first property is delimited with;instead of a comma, and the second misses the comma completely. – Bergi Commented Aug 5, 2015 at 20:36