Reactjs 添加道具以使用循环构造响应渲染调用
假设我有一个数组(“道具”),我想用它来渲染React组件。比如:Reactjs 添加道具以使用循环构造响应渲染调用,reactjs,react-jsx,Reactjs,React Jsx,假设我有一个数组(“道具”),我想用它来渲染React组件。比如: const props = []; ReactDOM.render(<Comp prop0={props[0]} prop1={props[1]/>, document.getElementById('foo')); const props=[]; ReactDOM.render(,document.getElementById('foo'); 是否有可接受的方法使用一些循环构造而不是“硬编码”将道具添加到Re
const props = [];
ReactDOM.render(<Comp prop0={props[0]} prop1={props[1]/>, document.getElementById('foo'));
const props=[];
ReactDOM.render(,document.getElementById('foo');
是否有可接受的方法使用一些循环构造而不是“硬编码”将道具添加到React组件?通常,您会在那里使用spread运算符:
var props = {};
props.foo = 'something';
props.bar = 'something else';
ReactDOM.render(<Component {...props} />, someDomElement);
var-props={};
props.foo='something';
props.bar=‘其他东西’;
render(,somedElement);
通常,您会在那里使用spread运算符:
var props = {};
props.foo = 'something';
props.bar = 'something else';
ReactDOM.render(<Component {...props} />, someDomElement);
var-props={};
props.foo='something';
props.bar=‘其他东西’;
render(,somedElement);
我想,如果你想用不同数量的道具同时呈现不同数量的孩子,你可以这样做:
const React = require('react');
module.exports = function(children){
return React.createClass({
renderChildren: function(){
return children.map(function(Child){
return (
<div>
<Child {...Child.propz}/>
</div>
)
});
},
render: function(){
return (
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<title>Title</title>
</head>
<body>
<div>
{this.renderChildren()}
</div>
</body>
</html>
)
}
});
};
const React=require('React');
module.exports=函数(子项){
返回React.createClass({
renderChildren:function(){
返回children.map(函数(子函数){
返回(
)
});
},
render:function(){
返回(
标题
{this.renderChildren()}
)
}
});
};
我想,如果你想用不同数量的道具同时呈现不同数量的孩子,你可以这样做:
const React = require('react');
module.exports = function(children){
return React.createClass({
renderChildren: function(){
return children.map(function(Child){
return (
<div>
<Child {...Child.propz}/>
</div>
)
});
},
render: function(){
return (
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<title>Title</title>
</head>
<body>
<div>
{this.renderChildren()}
</div>
</body>
</html>
)
}
});
};
const React=require('React');
module.exports=函数(子项){
返回React.createClass({
renderChildren:function(){
返回children.map(函数(子函数){
返回(
)
});
},
render:function(){
返回(
标题
{this.renderChildren()}
)
}
});
};
完全正确。可以添加此链接以供参考:谢谢您的回答!你知道怎么做同样的事情,但是有多个孩子和多个道具吗?没错。可以添加此链接以供参考:谢谢您的回答!你知道如何做同样的事情,但有多个孩子以及多个道具?