proptypes array of objects code example

Example 1: proptypes array of objects

MyComponent.propTypes = {
  items: PropTypes.arrayOf(
    PropTypes.shape({
      code: PropTypes.string,
      id: PropTypes.number,
    })
  ),
};

Example 2: react proptypes

import PropTypes from 'prop-types';

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.propTypes = {
  name: PropTypes.string
};

Example 3: proptypes shape

PropTypes.shape({ name: PropTypes.string, age: PropTypes.number })

Example 4: proptypes.arrayof/()

optionalArrayOf: PropTypes.arrayOf(PropTypes.number)

Example 5: react propthpes or

size: PropTypes.oneOfType([
  PropTypes.string,
  PropTypes.number
]),

Tags:

Misc Example