Skip to content

Instantly share code, notes, and snippets.

@vitorbal
Created January 19, 2017 10:51
Show Gist options
  • Save vitorbal/72873cb6f9f77540741147a917529aef to your computer and use it in GitHub Desktop.
Save vitorbal/72873cb6f9f77540741147a917529aef to your computer and use it in GitHub Desktop.
Best practices for writing PropTypes for React Components (Work in Progress)

Best Practices for React PropTypes

  • Avoid using PropTypes.string for a prop that sets a type/variation, use PropTypes.oneOf(CONSTANT) instead:
// BAD:
Button.propTypes = {
    size: React.PropTypes.string
};

// Good:
const SIZES = {
    small: '25px',
    standard: '32px',
    big: '50px'
};

Button.propTypes = {
    size: React.PropTypes.oneOf(Object.keys(SIZES))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment