Airbnb React/JSX Style Guide

A mostly reasonable approach to React and JSX

This style guide is mostly based on the standards that are currently prevalent in JavaScript, although some conventions (i.e async/await or static class fields) may still be included or prohibited on a case-by-case basis. Currently, anything prior to stage 3 is not included nor recommended in this guide.

Table of Contents

Basic Rules

Class vs React.createClass vs stateless

Mixins

Why? Mixins introduce implicit dependencies, cause name clashes, and cause snowballing complexity. Most use cases for mixins can be accomplished in better ways via components, higher-order components, or utility modules.

Naming

  • Extensions: Use .jsx extension for React components. eslint: react/jsx-filename-extensionarrow-up-right

  • Filename: Use PascalCase for filenames. E.g., ReservationCard.jsx.

  • Reference Naming: Use PascalCase for React components and camelCase for their instances. eslint: react/jsx-pascal-casearrow-up-right

  • Component Naming: Use the filename as the component name. For example, ReservationCard.jsx should have a reference name of ReservationCard. However, for root components of a directory, use index.jsx as the filename and use the directory name as the component name:

  • Higher-order Component Naming: Use a composite of the higher-order component’s name and the passed-in component’s name as the displayName on the generated component. For example, the higher-order component withFoo(), when passed a component Bar should produce a component with a displayName of withFoo(Bar).

    Why? A component’s displayName may be used by developer tools or in error messages, and having a value that clearly expresses this relationship helps people understand what is happening.

  • Props Naming: Avoid using DOM component prop names for different purposes.

    Why? People expect props like style and className to mean one specific thing. Varying this API for a subset of your app makes the code less readable and less maintainable, and may cause bugs.

Declaration

  • Do not use displayName for naming components. Instead, name the component by reference.

Alignment

Quotes

  • Always use double quotes (") for JSX attributes, but single quotes (') for all other JS. eslint: jsx-quotesarrow-up-right

    Why? Regular HTML attributes also typically use double quotes instead of single, so JSX attributes mirror this convention.

Spacing

Props

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

Why? Not using a stable ID is an anti-patternarrow-up-right 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.

  • 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.

  • 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 DOMarrow-up-right.

Exceptions:

  • HOCs that proxy down props and hoist propTypes

  • Spreading objects with known, explicit props. This can be particularly useful when testing React components with Mocha’s beforeEach construct.

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

Refs

Parentheses

Tags

Methods

  • Use arrow functions to close over local variables. It is handy when you need to pass additional data to an event handler. Although, make sure they do not massively hurt performancearrow-up-right, in particular when passed to custom components that might be PureComponents, because they will trigger a possibly needless rerender every time.

  • Bind event handlers for the render method in the constructor. eslint: react/jsx-no-bindarrow-up-right

    Why? A bind call in the render path creates a brand new function on every single render. Do not use arrow functions in class fields, because it makes them challenging to test and debug, and can negatively impact performancearrow-up-right, and because conceptually, class fields are for data, not logic.

  • Do not use underscore prefix for internal methods of a React component.

    Why? Underscore prefixes are sometimes used as a convention in other languages to denote privacy. But, unlike those languages, there is no native support for privacy in JavaScript, everything is public. Regardless of your intentions, adding underscore prefixes to your properties does not actually make them private, and any property (underscore-prefixed or not) should be treated as being public. See issues #1024arrow-up-right, and #490arrow-up-right for a more in-depth discussion.

  • Be sure to return a value in your render methods. eslint: react/require-render-returnarrow-up-right

Ordering

  • Ordering for class extends React.Component:

  1. optional static methods

  2. constructor

  3. getChildContext

  4. componentWillMount

  5. componentDidMount

  6. componentWillReceiveProps

  7. shouldComponentUpdate

  8. componentWillUpdate

  9. componentDidUpdate

  10. componentWillUnmount

  11. event handlers starting with 'handle' like handleSubmit() or handleChangeDescription()

  12. event handlers starting with 'on' like onClickSubmit() or onChangeDescription()

  13. getter methods for render like getSelectReason() or getFooterContent()

  14. optional render methods like renderNavigation() or renderProfilePicture()

  15. render

  1. displayName

  2. propTypes

  3. contextTypes

  4. childContextTypes

  5. mixins

  6. statics

  7. defaultProps

  8. getDefaultProps

  9. getInitialState

  10. getChildContext

  11. componentWillMount

  12. componentDidMount

  13. componentWillReceiveProps

  14. shouldComponentUpdate

  15. componentWillUpdate

  16. componentDidUpdate

  17. componentWillUnmount

  18. clickHandlers or eventHandlers like onClickSubmit() or onChangeDescription()

  19. getter methods for render like getSelectReason() or getFooterContent()

  20. optional render methods like renderNavigation() or renderProfilePicture()

  21. render

isMounted

Why? isMounted is an anti-patternarrow-up-right, is not available when using ES6 classes, and is on its way to being officially deprecated.

Translation

This JSX/React style guide is also available in other languages:

⬆ back to top

Last updated