Reactjs 如何验证应接受组件的react prop
假设我希望我的组件接受类型为Reactjs 如何验证应接受组件的react prop,reactjs,react-proptypes,Reactjs,React Proptypes,假设我希望我的组件接受类型为React.component const MyComponent = ({ child: Child }) => <div> <Child {...childProps} /> </div> MyComponent.propTypes = { child: PropTypes.[???].isRequired } constmycomponent=({child:child})=> MyCompon
React.component
const MyComponent = ({ child: Child }) =>
<div>
<Child {...childProps} />
</div>
MyComponent.propTypes = {
child: PropTypes.[???].isRequired
}
constmycomponent=({child:child})=>
MyComponent.propTypes={
子项:PropTypes.[??].isRequired
}
道具类型
中是否有我可以使用的React.Component
验证器
edit:我尝试了PropTypes.element
,但出现了一个错误
失败的道具类型:为“测试”提供的“功能”类型的无效道具“el”,应为单个元素。
edit2:我刚在
使用
元素
属性
MyComponent.propTypes = {
child: PropTypes.element.isRequired
}
或instanceOf
MyComponent.propTypes = {
child: PropTypes.instanceOf(ReactComponentName).isRequired
}
使用
元素
属性
MyComponent.propTypes = {
child: PropTypes.element.isRequired
}
或instanceOf
MyComponent.propTypes = {
child: PropTypes.instanceOf(ReactComponentName).isRequired
}
我尝试过使用
.element
,但它似乎是在验证元素,而不是组件,我希望我的组件能够灵活地接受任何有效的React.component并使用相应的属性呈现它。我尝试过使用.element
,但它似乎是在验证元素,不是组件,我希望我的组件是灵活的,可以接受任何有效的React.component,并使用相应的道具进行渲染