mat*_*tts 0 arrays reactjs react-proptypes
我有一个数组对象作为道具传递,数组看起来像:
[
{
"title": "eat food"
},
{
"title": "Drinks",
"sub_items": [
{
"title": "Beer",
"isDrinking": true
}
]
},
{
"title": "eat Pizza"
},
{
"title": "Other Drinks",
"sub_items": [
{
"title": "Soda",
"isDrinking": false
},
{
"title": "Soda",
"isDrinking": false
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我想要做的是添加 propTypes 验证,比如
标题:PropTypes.string.isRequired
sub_items : PropTypes.array
sub_items : 数组内的道具验证,例如 title sting 和 isDrinking 布尔值。
请注意如何在数组上实现这一点。(ps。我的reactjs知识非常有限,所以如果我问一个明显的愚蠢问题,请原谅我)
你可以写:
PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string.isRequired,
sub_items: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string.isRequired,
isDrinking: PropTypes.bool.isRequired
})
})).isRequired
Run Code Online (Sandbox Code Playgroud)
请注意 howsub_items不是必需的,但如果它包含在其中一个对象中,则它必须是具有title和isDrinking定义的对象数组。
| 归档时间: |
|
| 查看次数: |
1068 次 |
| 最近记录: |