question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
zpaocommented, Apr 18, 2015

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

4reactions
zpaocommented, Apr 20, 2015

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
React PropTypes arrayOf oneOf shape A or shape B
I decided to post my update to the question as an answer. I found the answer here. I should be using OneofType instead....
Read more >
How to use the prop-types.oneOf function in prop-types - Snyk
To help you get started, we've selected a few prop-types examples, based on popular ways it is used in public projects. ; arrowLeft:...
Read more >
React Prop Types with TypeScript | Ben Ilegbodu
A guide comparing React Prop Types to their equivalent TypeScript ... you need to figure out is how to define the prop types...
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal ... oneOfType([ isEmail, PropTypes.shape({ address: isEmail }) ]) }.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found