bigblind bigblind - 1 year ago 126
React JSX Question

only allow children of a specific type in a react component

I have a

component and a
component, and I'd like to throw an error when
has children that aren't
components. Is this possible, or am I trying to solve the wrong problem?

Answer Source

You can use the displayName for each child, accessed via type:

for (child in this.props.children){
  if (this.props.children[child].type.displayName != 'Card'){
    console.log("Warning CardGroup has children that aren't Card components");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download