PropTypes.oneOf([PropTypes.shape(...)])
See original GitHub issueI 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 toC
, expected one of [null,null].
Is this a bug, limitation, or am I doing it wrong?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think you want
PropTypes.oneOfType
.oneOf
is basically an enum.No problem. You’re not the first one to do that (and I’m willing to bet you won’t be the last 😃 )