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 - ' },
]
};