React prop validation: Multiple finite prop types?

574 views Asked by At

I like to set empty array and object variables as false, I feel like it makes my code (!messier). I was wondering if there was a way to validate a finite set of prop types. Something like this:

const propTypes = {
  example: PropTypes.oneOf([PropTypes.array, PropTypes.bool]).isRequired
}

I don't mind being told why this is a bad idea, I appreciate all the constructive comments.

1

There are 1 answers

1
Patrik Krhovský On

Better way is set array (object) as null.

let example = null;

propTypes = {
    example: PropTypes.array.isRequired
}

if (!example) { ... }