ReactJs: What should the PropTypes be for this.props.children?

前端 未结 8 1121
梦如初夏
梦如初夏 2020-12-04 05:49

Given a simple component that renders its children:

class ContainerComponent extends Component {
  static propTypes = {
    children: PropTypes.object.isRequ         


        
8条回答
  •  不思量自难忘°
    2020-12-04 06:33

    If you want to match exactly a component type, check this

    MenuPrimary.propTypes = {
      children: PropTypes.oneOfType([
        PropTypes.arrayOf(MenuPrimaryItem),
        PropTypes.objectOf(MenuPrimaryItem)
      ])
    }
    

    If you want to match exactly some component types, check this

    const HeaderTypes = [
      PropTypes.objectOf(MenuPrimary),
      PropTypes.objectOf(UserInfo)
    ]
    
    Header.propTypes = {
      children: PropTypes.oneOfType([
        PropTypes.arrayOf(PropTypes.oneOfType([...HeaderTypes])),
        ...HeaderTypes
      ])
    }
    

提交回复
热议问题