Blog.

How to Require Child Props in React

Cover image for How to Require Child Props in React

How to Require Child Props in React

How to require child props in react

You have a component and the child of that component needs to be one or more HTML elements. How do you properly set the prop types for this?

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

Or

static propTypes = {
  children: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node
  ]).isRequired
}

The first will require that there is a single node element as the child of the component. The second requires that there is one or more node elements as the child of the component.

I hope this helps. If it did, let me know.

Resources