【发布时间】:2017-06-26 15:01:10
【问题描述】:
给定一个呈现其子级的简单组件:
class ContainerComponent extends Component {
static propTypes = {
children: PropTypes.object.isRequired,
}
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
export default ContainerComponent;
问题:children prop 的 propType 应该是什么?
当我将它设置为一个对象时,当我使用带有多个子组件的组件时它会失败:
<ContainerComponent>
<div>1</div>
<div>2</div>
</ContainerComponent>
警告:失败的道具类型:无效的道具
children类型为array提供给ContainerComponent,预期object。
如果我将它设置为一个数组,如果我只给它一个孩子,它会失败,即:
<ContainerComponent>
<div>1</div>
</ContainerComponent>
警告:道具类型失败:对象类型的道具子对象无效 提供给 ContainerComponent,预期的数组。
请告知,我是否应该不费心对子元素进行 propTypes 检查?
【问题讨论】:
-
你可能想要
node -
请看下面我的回答,其中描述了更多选项,但是,如果您正在寻找组件子组件,那么它就是 PropTypes.element。 PropTypes.node 描述了任何可以渲染的东西——字符串、数字、元素或这些东西的数组。如果这适合你,那么这就是方式。
标签: reactjs jsx react-proptypes