Search code examples
reactjsreact-proptypes

PropTypes oneOfType isRequired


I have a component that expects propType oneOfType bool or object so I wrote:

processInfoValues: PropTypes.oneOfType([
   PropTypes.bool,
   PropTypes.object,
])

which is working fine, my problem is that this prop should be required, how can I achieve that?

I tried like that:

processInfoValues: PropTypes.oneOfType([
  PropTypes.bool.isRequired,
  PropTypes.object.isRequired,
])

and I keep getting this error:

propType "processInfoValues" is not required, but has no corresponding defaultProps declaration

What am I doing wrong?


Solution

  • I think this is happening because you are setting isRequired on the types inside of the oneOfType. You should set it on the outer PropTypes. I think this might work

    processInfoValues: PropTypes.oneOfType([
      PropTypes.bool,
      PropTypes.object,
    ]).isRequired