Props

  • Always use camelCase for prop names.

    1. // bad
    2. <Foo
    3. UserName="hello"
    4. phone_number={12345678}
    5. />
    6. // good
    7. <Foo
    8. userName="hello"
    9. phoneNumber={12345678}
    10. />
  • Omit the value of the prop when it is explicitly true. eslint: react/jsx-boolean-value

    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> alt props. eslint: jsx-a11y/img-redundant-alt

    Why? Screenreaders already announce img elements as images, so there is no need to include this information in the alt text.

    1. // bad
    2. <img src="hello.jpg" alt="Picture of me waving hello" />
    3. // good
    4. <img src="hello.jpg" alt="Me waving hello" />
  • Use only valid, non-abstract ARIA roles. eslint: jsx-a11y/aria-role

    1. // bad - not an ARIA role
    2. <div role="datepicker" />
    3. // bad - abstract ARIA role
    4. <div role="range" />
    5. // good
    6. <div role="button" />
  • Do not use accessKey on elements. eslint: jsx-a11y/no-access-key

    Why? Inconsistencies between keyboard shortcuts and keyboard commands used by people using screenreaders and keyboards complicate accessibility.

    1. // bad
    2. <div accessKey="h" />
    3. // good
    4. <div />
  • Avoid using an array index as key prop, prefer a stable ID.

Why? Not using a stable ID @robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318">is an anti-pattern because it can negatively impact performance and cause issues with component state.

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

  1. // bad
  2. {todos.map((todo, index) =>
  3. <Todo
  4. {...todo}
  5. key={index}
  6. />
  7. )}
  8. // good
  9. {todos.map(todo => (
  10. <Todo
  11. {...todo}
  12. key={todo.id}
  13. />
  14. ))}
  • Always define explicit defaultProps for all non-required props.

    Why? propTypes are a form of documentation, and providing defaultProps means the reader of your code doesn’t have to assume as much. In addition, it can mean that your code can omit certain type checks.

    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. return <div>{foo}{bar}{children}</div>;
    13. }
    14. SFC.propTypes = {
    15. foo: PropTypes.number.isRequired,
    16. bar: PropTypes.string,
    17. children: PropTypes.node,
    18. };
    19. SFC.defaultProps = {
    20. bar: '',
    21. children: null,
    22. };
  • Use spread props sparingly.

    Why? Otherwise you’re more likely to pass unnecessary props down to components. And for React v15.6.1 and older, you could pass invalid HTML attributes to the DOM.

    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. export default function Foo {
    2. const props = {
    3. text: '',
    4. isPublished: false
    5. }
    6. return (<div {...props} />);
    7. }

    Notes for use:
    Filter out unnecessary props when possible. Also, use prop-types-exact to help prevent bugs.

    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. return <WrappedComponent {...relevantProps} />
    10. }