[英]React - not repeat a component
I have the following react component for a dropdown: 我有以下用于下拉的react组件:
var UIDropdown = React.createClass({
getDefaultProps: function () {
return {
isOpen: false
};
},
render: function () {
if (this.props.isOpen) {
return (
<div className="dropdown">
<ul className="uk-nav uk-nav-dropdown tm-svg-center">
{this.props.mapOpacityValues.map(function (list, i) {
return (
<li onClick={this.props.opacityThermatic.bind(this, list) } key={"list" + i}><a href="javascript:void(0)">{`${list * 100}%`}</a></li>
);
}, this) }
</ul>
</div>
);
}
return null;
}
});
I'm looping through some data which outputs some list items, but I have a number for different data items. 我正在遍历一些输出一些列表项的数据,但是我有一个用于不同数据项的数字。
How can I add the following in the component without repeating the dropdown component code: 如何在组件中添加以下内容而不重复下拉组件代码:
{this.props.mapOpacityValues.map(function (list, i) {
return (
<li onClick={this.props.opacityThermatic.bind(this, list) } key={"list" + i}><a href="javascript:void(0)">{`${list * 100}%`}</a></li>
);
}, this) }
Example but with a single dropdown component https://jsfiddle.net/zidski/ddLdg84s/ 示例,但具有单个下拉组件https://jsfiddle.net/zidski/ddLdg84s/
If I understand you correctly, you need to reuse your Dropdown
component 如果我对您的理解正确,则需要重用
Dropdown
组件
Then do something like this 然后做这样的事情
DropdownItems component DropdownItems组件
var DropdownItems = React.createClass({
render: function () {
return(
<ul className="uk-nav uk-nav-dropdown tm-svg-center">
{this.props.mapOpacityValues.map(function (list, i) {
return (
<li onClick={this.props.opacityThermatic.bind(this, list) } key={"list" + i}><a href="javascript:void(0)">{`${list * 100}%`}</a></li>
);
}, this)
}
</ul>
)
}
});
UIDropdown component UIDropdown组件
var UIDropdown = React.createClass({
getDefaultProps: function () {
return {
isOpen: false
};
},
render: function () {
if (this.props.isOpen) {
return (
<div className="dropdown">
{this.props.children}
</div>
);
}
return null;
}
});
Then you can create any number UIDropdowns
like 然后,您可以创建任意数量的
UIDropdowns
例如
<UIDropdown>
<DropdownItems mapOpacityValues={someData} opacityThermatic={someFunction}>
</UIDropdown>
Here you need to repeat neither dropdown
nor li
items. 在这里,您无需重复
dropdown
或li
项目。 You just resue them in your implementation. 您只需在实现中恢复它们即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.