• Omit the value of the prop when it is explicitly . eslint:

    1. // bad
    2. <Foo
    3. hidden={true}
    4. />
    5. // good
    6. <Foo
    7. hidden
    8. />
    9. // good
    10. <Foo hidden />
  • Always include an alt prop on <img> tags. If the image is presentational, alt can be an empty string or the <img> must have role="presentation". eslint: jsx-a11y/alt-text

    1. // bad
    2. <img src="hello.jpg" />
    3. // good
    4. <img src="hello.jpg" alt="Me waving hello" />
    5. // good
    6. <img src="hello.jpg" alt="" />
    7. // good
    8. <img src="hello.jpg" role="presentation" />
  • Do not use words like “image”, “photo”, or “picture” in <img> props. eslint:

  • Use only valid, non-abstract ARIA roles. eslint:

    1. // bad - not an ARIA role
    2. <div role="datepicker" />
    3. <div role="range" />
    4. // good
    5. <div role="button" />
    1. // bad
    2. <div accessKey="h" />
    3. // good
    4. <div />
  • Avoid using an array index as key prop, prefer a stable ID.

We don’t recommend using indexes for keys if the order of items may change.

  • Always define explicit defaultProps for all non-required props.

    1. // bad
    2. function SFC({ foo, bar, children }) {
    3. return <div>{foo}{bar}{children}</div>;
    4. }
    5. SFC.propTypes = {
    6. foo: PropTypes.number.isRequired,
    7. bar: PropTypes.string,
    8. children: PropTypes.node,
    9. };
    10. // good
    11. function SFC({ foo, bar, children }) {
    12. }
    13. SFC.propTypes = {
    14. foo: PropTypes.number.isRequired,
    15. bar: PropTypes.string,
    16. children: PropTypes.node,
    17. SFC.defaultProps = {
    18. bar: '',
    19. children: null,
    20. };
  • Use spread props sparingly.

    Exceptions:

  • HOCs that proxy down props and hoist propTypes

    1. function HOC(WrappedComponent) {
    2. return class Proxy extends React.Component {
    3. Proxy.propTypes = {
    4. text: PropTypes.string,
    5. isLoading: PropTypes.bool
    6. };
    7. render() {
    8. return <WrappedComponent {...this.props} />
    9. }
    10. }
    11. }
  • Spreading objects with known, explicit props. This can be particularly useful when testing React components with Mocha’s beforeEach construct.

    1. // bad
    2. render() {
    3. const { irrelevantProp, ...relevantProps } = this.props;
    4. return <WrappedComponent {...this.props} />
    5. }
    6. // good
    7. render() {
    8. const { irrelevantProp, ...relevantProps } = this.props;
    9. }