gpt4 book ai didi

javascript - React JS 'propTypes' 验证器运行两次

转载 作者:行者123 更新时间:2023-11-29 19:21:23 25 4
gpt4 key购买 nike

下面的代码创建有序和无序列表。虽然其他部分代码因为无关紧要就不贴在这里了

问题:我通过在“导航”组件中调用它来将一些属性传递给“列表”组件。我正在通过“列表”的 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);

最佳答案

这是因为React在0.11引入了描述符这个问题在 v0.12 中仍然存在。下面的链接对此进行了更多讨论。

React Github issue on this

React v0.11 blog on propValidation

关于javascript - React JS 'propTypes' 验证器运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32873286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com