prop-types icon indicating copy to clipboard operation
prop-types copied to clipboard

Specify Valid Children Component Types

Open ambroselittle opened this issue 8 years ago • 4 comments

I was expecting to be able to use PropTypes.instanceOfto specify valid children types, like so:

    static propTypes = {
        children: PropTypes.arrayOf(PropTypes.oneOfType([ 
          PropTypes.instanceOf(ComponentOne), 
          PropTypes.instanceOf(ComponentTwo)
        ]))
    }

I got a relatively obscure error: Failed prop type: Right-hand side of 'instanceof' is not an object

After some searching, stumbled on: https://github.com/facebook/react/issues/2979

The explanation/reason for closing being that instanceOf doesn't work on components. Okay, so would still like to achieve the end goal somehow. https://github.com/facebook/react/issues/2979#issuecomment-219610493 is actually pretty slick.

Just specifying PropTypes.element is not sufficient, IMO.

I'm assuming this is a feature request, but maybe I'm overlooking something. TIA.

ambroselittle avatar May 18 '17 22:05 ambroselittle

In the meantime, try childrenOfType in https://npmjs.com/airbnb-prop-types.

ljharb avatar Mar 20 '19 23:03 ljharb

Is this open for a PR? Or are there things we still have to discuss/agree on? Would be nice to have this.

ismay avatar Sep 04 '19 13:09 ismay

It doesn’t have the help wanted label, so it’s not open for PR (ie, you can make one, but there’s no guarantee it’d be accepted)

ljharb avatar Sep 04 '19 14:09 ljharb

I see. Why is there no help wanted label if I may ask? Are there objections to adding this?

ismay avatar Sep 04 '19 15:09 ismay