14
React中有一种方法可以为特定形状的项目的嵌套数组提供默认道具吗?如何为React中的嵌套形状提供默认道具?
给出下面的例子,我可以看到第一次尝试,但是这并不像预期的那样工作。
static propTypes = {
heading: PT.string,
items: PT.arrayOf(PT.shape({
href: PT.string,
label: PT.string,
})).isRequired,
};
static defaultProps = {
heading: 'this works',
items: [{
href: '/',
label: ' - this does not - ',
}],
};
在这个例子中,我希望以下内容:
// Given these props
const passedInProps = {
items: [{ href: 'foo', href: 'bar' }]
};
// Would resolve to:
const props = {
heading: 'this works',
items: [
{ href: 'foo', label: ' - this does not - ' },
{ href: 'bar', label: ' - this does not - ' },
]
};