patternreactCritical
React PropTypes: Allow different types of PropTypes for one prop
Viewed 0 times
propproptypesonereacttypesdifferentforallow
Problem
I have a component that receives a prop for its size. The prop can be either a string or a number ex:
Can I let
If I don't specify the type I get a warning:
prop type
React.PropTypes.
"LARGE" or 17.Can I let
React.PropTypes know that this can be either one or the other in the propTypes validation?If I don't specify the type I get a warning:
prop type
size is invalid; it must be a function, usually fromReact.PropTypes.
MyComponent.propTypes = {
size: React.PropTypes
}Solution
size: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),Learn more: Typechecking With PropTypes
Code Snippets
size: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),Context
Stack Overflow Q#41808428, score: 836
Revisions (0)
No revisions yet.