How React PropTypes allow different type for one prop?

  • PropTypes does not allow different types for one prop
  • You need to define separate PropTypes for each type
  • You can use PropTypes.oneOfType()
You can use PropTypes.oneOfType() to allow a prop to have multiple possible types. For example, you can define a prop that can be either a string or a number like this: 'myProp: PropTypes.oneOfType([PropTypes.string, PropTypes.number])'. This will allow the prop to accept values of either type, and will throw a warning if a value of a different type is passed in.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *