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

前端 未结 8 1112
梦如初夏
梦如初夏 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:55

    Try something like this utilizing oneOfType or PropTypes.node

    import PropTypes from 'prop-types'
    
    ...
    
    static propTypes = {
        children: PropTypes.oneOfType([
            PropTypes.arrayOf(PropTypes.node),
            PropTypes.node
        ]).isRequired
    }
    

    or

    static propTypes = {
        children: PropTypes.node.isRequired,
    }
    

提交回复
热议问题