admin管理员组文章数量:1323529
var data = [
{author: 'foo', ment: 'nice'},
{author: 'bar', ment: 'wow'}
];
var CommentBox = React.createClass({
render: function () {
var CommentNodes = this.props.data.map(function (ment) {
return (
<Comment author={ment.author} ment={mentment}>
</Comment>
);
});
return (
<div className="ment-box">
{CommentNodes}
</div>
);
}
});
var Comment = React.createClass({
render: function () {
return (
<div className="ment-box ment">
<h2 className="ment-author">
{this.props.author}
</h2>
{this.propsment}
</div>
);
}
});
React.render(<CommentBox data={data}/>, document.getElementById("example"));
In the piece of code, I just pass the parameter to Comment
using data
. Since data
is a object
, which similar to Python's dict
. So I'm wondering, can I just pass data
as the unpacking object
? Like using **
is Python:
>>> def show(**kwargs):
... return kwargs
...
>>> items = {'a': 1, 'b': 2}
>>> print(show(**items))
{'a': 1, 'b': 2}
var data = [
{author: 'foo', ment: 'nice'},
{author: 'bar', ment: 'wow'}
];
var CommentBox = React.createClass({
render: function () {
var CommentNodes = this.props.data.map(function (ment) {
return (
<Comment author={ment.author} ment={ment.ment}>
</Comment>
);
});
return (
<div className="ment-box">
{CommentNodes}
</div>
);
}
});
var Comment = React.createClass({
render: function () {
return (
<div className="ment-box ment">
<h2 className="ment-author">
{this.props.author}
</h2>
{this.props.ment}
</div>
);
}
});
React.render(<CommentBox data={data}/>, document.getElementById("example"));
In the piece of code, I just pass the parameter to Comment
using data
. Since data
is a object
, which similar to Python's dict
. So I'm wondering, can I just pass data
as the unpacking object
? Like using **
is Python:
>>> def show(**kwargs):
... return kwargs
...
>>> items = {'a': 1, 'b': 2}
>>> print(show(**items))
{'a': 1, 'b': 2}
Share
Improve this question
edited Jan 31, 2016 at 3:26
Dmitry Shvedov
3,2964 gold badges40 silver badges56 bronze badges
asked Sep 15, 2015 at 18:35
user2394303user2394303
1
- Looks like data is an array and this.props.data.map is unpacking the array. – J. Mark Stevens Commented Sep 15, 2015 at 21:27
2 Answers
Reset to default 6As @AlexPalcuie answers above you can use object spread to acplish exactly what python **
operators do.
So this is equivalent to your code above:
var CommentBox = React.createClass({
render: function () {
var CommentNodes = this.props.data.map(function (ment) {
return (
<Comment {...ment}>
</Comment>
);
});
return (
<div className="ment-box">
{CommentNodes}
</div>
);
}
});
You can use the spread attributes syntax.
本文标签: pythonCan I pass unpacking object as parameter in Javascript or JSXStack Overflow
版权声明:本文标题:python - Can I pass unpacking object as parameter in Javascript or JSX? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128753a2422062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论