[英]Is isRequired necessary for nested PropTypes?
Say I have this PropType defined: 假设我已定义此PropType:
Component.propTypes = {
complicatedData: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
data: PropTypes.arrayOf(PropTypes.number)
})
).isRequired,
};
If that data structure is required, is the isRequired
attributed necessary for every level of that nested structure, or does the top-level isRequired
at the end encompass everything inside? 如果需要该数据结构,则该嵌套结构的每个级别的
isRequired
属性是必需的,还是isRequired
的顶层isRequired
包含内部的所有内容?
The top level isRequired
only checks if complicatedData
is provided and is an array. 顶层
isRequired
如果仅检查complicatedData
被提供并且是一个数组。 You can give it an array with empty objects such as [{}, {}]
and it'll pass the test based on your code. 您可以给它一个包含空对象(例如
[{}, {}]
的数组,它会根据您的代码通过测试。
If you want each element in your array complicatedData
to have a name
property, then you need to write: 如果您希望数组中的每个元素
complicatedData
都具有name
属性,则需要编写:
Component.propTypes = {
complicatedData: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string.isRequired,
data: PropTypes.arrayOf(PropTypes.number)
})
).isRequired,
};
Same if you want each element to have a data
property. 如果希望每个元素都具有
data
属性,则相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.