React PropTypes: Allow different types of PropTypes for one prop
Solution 1:
size: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
Learn more: Typechecking With PropTypes
Solution 2:
For documentation purpose, it's better to list the string values that are legal:
size: PropTypes.oneOfType([
PropTypes.number,
PropTypes.oneOf([ 'SMALL', 'LARGE' ]),
]),
Solution 3:
This might work for you:
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
Solution 4:
Here is pro example of using multi proptypes and single proptype.
import React, { Component } from 'react';
import { string, shape, array, oneOfType } from 'prop-types';
class MyComponent extends Component {
/**
* Render
*/
render() {
const { title, data } = this.props;
return (
<>
{title}
<br />
{data}
</>
);
}
}
/**
* Define component props
*/
MyComponent.propTypes = {
data: oneOfType([array, string, shape({})]),
title: string,
};
export default MyComponent;