Common React PropTypes Examples

Basic

MyComp.propTypes = {  
  email:     PropTypes.string.isRequired,
  seats:     PropTypes.number,
  settings:  PropTypes.object,
  callback:  PropTypes.func,
  isClosed:  PropTypes.bool,
  any:       PropTypes.any,
};

Style and Children

What PropType should you use to check the vailidity of this.props.children and this.props.style?

MyComp.propTypes = {  
  // Use this for style
  style: PropTypes.any,

  // Use this for children
  children: PropTypes.node,

  // Use this for nested children
  children: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
  ]),
};

Elements

MyComp.propTypes = {  
  element: PropTypes.element, // react element
  node:    PropTypes.node,    // num, string, element
};

Enumerables

MyComp.propTypes = {  
  enum:  PropTypes.oneOf(['M','F']),  // enum
  union: PropTypes.oneOfType([        // any
    PropTypes.string,
    PropTypes.number,
  ]),
};

Arrays & Objects

MyComp.propTypes = {  
  array:    PropTypes.array,
  arrayOf:  PropTypes.arrayOf(PropTypes.number),
  object:   PropTypes.object,
  objectOf: PropTypes.objectOf(PropTypes.number),

  message:  PropTypes.instanceOf(Message),

  object2:  PropTypes.shape({
    color:  PropTypes.string,
    size:   PropTypes.number,
  }),
};

References

A set of UI components for React Native.
Get Carbon Native!