给定一个简单的组件来呈现其子对象:
class ContainerComponent extends Component {
static propTypes = {
children: PropTypes.object.isRequired,
}
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
export default ContainerComponent;
Question: What should the propType of the children prop be?
当我将其设置为对象时,当我将组件与多个子组件一起使用时,它会失败:
<ContainerComponent>
<div>1</div>
<div>2</div>
</ContainerComponent>
警告:失败的props 类型:
array
类型的propschildren
无效
如果我将其设置为数组,如果我只给它一个子元素,它将失败,即:
<ContainerComponent>
<div>1</div>
</ContainerComponent>
警告:失败的props 类型:无效的props 子对象类型
请告诉我,我是否应该省心做一个关于 children 元素的propTypes判断?