【发布时间】:2020-01-09 07:33:40
【问题描述】:
我有一个组件可以克隆孩子并将一些新的道具传递给它。 在我的 PropTypes 中,我只想允许 React 组件而不是其他任何东西。 例如:
<PartentComponent>
<ChildContent/>
</PartentComponent>
它并不总是被称为 ChildContent,它可以是组件的任何名称。
我不希望 parentComponent 允许除组件之外的任何内容。 这是不允许的。
<PartentComponent>
<div>content</div>
</PartentComponent>
我尝试在 ParentComponent 中使用 PropType 元素。但这仍然接受一个 div
Modal.propTypes = {
children: PropTypes.element,
};
除了组件我什么都不想要的原因是我在 cloneElement 中传递了不能在 div 上使用的道具。
const childComponent = React.Children.map(children, child =>
cloneElement(child, {
end: onEndProcesss,
}));
【问题讨论】:
-
您可以通过为“propTypes”对象中的键提供函数来添加自定义道具类型。