下面的代码创建有序和无序列表。虽然其他部分的代码因为无关紧要所以这里就不贴了
问题:我通过在“导航”组件中调用它来将一些属性传递给“列表”组件。我正在通过“列表”的 propTypes 运行一些验证来验证“列表”收到的项目。但是,我注意到验证运行了两次。我想不通为什么?
是不是因为代码内部发生了一些竞争条件。或者,它是 React 中的错误吗?
var Navigation = React.createClass({
render: function() {
return (
<nav>
<List type="ol" items={this.props.items} />
</nav>
);
}
});
var List = React.createClass({
propTypes: {
type: React.PropTypes.string.isRequired,
items: React.PropTypes.array.isRequired,
customProp: function(props, propName, componentName) {
if (props["type"] !== "ol" && props["type"] !== "ul") {
return new Error("Incorrect list type provided");
}
}
},
getInitialState: function() {
return {
showList: true,
listType: this.props.type
}
},
render: function() {
return (
<this.state.listType>
{this.props.items.map(function(item) {
return <ListItem data={item} />
})}
</this.state.listType>
)
}
});
React.render(<Navigation items={items} />, document.body);