React: PropTypes.oneOf([PropTypes.shape(...)])

Created on 18 Apr 2015  路  5Comments  路  Source: facebook/react

I have a component that expects one type of object or another. I've tried variations of this:

C.propTypes = {
  foo: PropTypes.oneOf([
    PropTypes.shape({
      x: PropTypes.string
    }).isRequired,

    PropTypes.shape({
      y: PropTypes.string
    })
  ]).isRequired
};

<C foo={{y: 'text'}} />

The warning:

Warning: Failed propType: Invalid prop foo of value [object Object] supplied to C, expected one of [null,null].

Is this a bug, limitation, or am I doing it wrong?

jsbin

Most helpful comment

I think you want PropTypes.oneOfType. oneOf is basically an enum.

All 5 comments

I think you want PropTypes.oneOfType. oneOf is basically an enum.

Oh darn, thanks.

No problem. You're not the first one to do that (and I'm willing to bet you won't be the last :) )

Life saver. Total Life saver. Thanks

Same here, I was super confused by the error message 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UnbearableBear picture UnbearableBear  路  3Comments

jimfb picture jimfb  路  3Comments

framerate picture framerate  路  3Comments

kocokolo picture kocokolo  路  3Comments

trusktr picture trusktr  路  3Comments