|
| 1 | +# Airbnb React/JSX Style Guide |
| 2 | + |
| 3 | +*A mostly reasonable approach to React and JSX* |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | + 1. [Basic Rules](#basic-rules) |
| 8 | + 1. [Naming](#naming) |
| 9 | + 1. [Declaration](#declaration) |
| 10 | + 1. [Alignment](#alignment) |
| 11 | + 1. [Quotes](#quotes) |
| 12 | + 1. [Spacing](#spacing) |
| 13 | + 1. [Props](#props) |
| 14 | + 1. [Parentheses](#parentheses) |
| 15 | + 1. [Tags](#tags) |
| 16 | + 1. [Ordering](#ordering) |
| 17 | + |
| 18 | +## Basic Rules |
| 19 | + |
| 20 | + - Only include one React component per file. |
| 21 | + - Always use JSX syntax. |
| 22 | + - Do not use `React.createElement` unless you're initializing the app from a `.js` file. |
| 23 | + |
| 24 | +## Naming |
| 25 | + |
| 26 | + - **Extensions**: Use `.jsx` extension for React components. |
| 27 | + - **Filename**: Use PascalCase for filenames. E.g., `ReservationCard.jsx`. |
| 28 | + - **Reference Naming**: Use PascalCase for React components and camelCase for their instances: |
| 29 | + ```javascript |
| 30 | + // bad |
| 31 | + const reservationCard = require('./ReservationCard'); |
| 32 | + |
| 33 | + // good |
| 34 | + const ReservationCard = require('./ReservationCard'); |
| 35 | + |
| 36 | + // bad |
| 37 | + const ReservationItem = <ReservationCard />; |
| 38 | + |
| 39 | + // good |
| 40 | + const reservationItem = <ReservationCard />; |
| 41 | + ``` |
| 42 | + |
| 43 | + **Component Naming**: Use the filename as the component name. So `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: |
| 44 | + ```javascript |
| 45 | + // bad |
| 46 | + const Footer = require('./Footer/Footer.jsx') |
| 47 | +
|
| 48 | + // bad |
| 49 | + const Footer = require('./Footer/index.jsx') |
| 50 | +
|
| 51 | + // good |
| 52 | + const Footer = require('./Footer') |
| 53 | + ``` |
| 54 | + |
| 55 | + |
| 56 | +## Declaration |
| 57 | + - Do not use displayName for naming components, instead name the component by reference. |
| 58 | + |
| 59 | + ```javascript |
| 60 | + // bad |
| 61 | + export default React.createClass({ |
| 62 | + displayName: 'ReservationCard', |
| 63 | + // stuff goes here |
| 64 | + }); |
| 65 | +
|
| 66 | + // good |
| 67 | + const ReservationCard = React.createClass({ |
| 68 | + // stuff goes here |
| 69 | + }); |
| 70 | + export default ReservationCard; |
| 71 | + ``` |
| 72 | + |
| 73 | +## Alignment |
| 74 | + - Follow these alignment styles for JSX syntax |
| 75 | + |
| 76 | + ```javascript |
| 77 | + // bad |
| 78 | + <Foo bar="bar" |
| 79 | + baz="baz" /> |
| 80 | +
|
| 81 | + // good |
| 82 | + <Foo |
| 83 | + bar="bar" |
| 84 | + baz="baz" |
| 85 | + /> |
| 86 | +
|
| 87 | + // if props fit in one line then keep it on the same line |
| 88 | + <Foo bar="bar" /> |
| 89 | +
|
| 90 | + // children get indented normally |
| 91 | + <Foo |
| 92 | + bar="bar" |
| 93 | + baz="baz" |
| 94 | + > |
| 95 | + <Spazz /> |
| 96 | + </Foo> |
| 97 | + ``` |
| 98 | + |
| 99 | +## Quotes |
| 100 | + - Always use double quotes (`"`) for JSX attributes, but single quotes for all other JavaScript. |
| 101 | + ```javascript |
| 102 | + // bad |
| 103 | + <Foo bar='bar' /> |
| 104 | +
|
| 105 | + // good |
| 106 | + <Foo bar="bar" /> |
| 107 | +
|
| 108 | + // bad |
| 109 | + <Foo style={{ left: "20px" }} /> |
| 110 | +
|
| 111 | + // good |
| 112 | + <Foo style={{ left: '20px' }} /> |
| 113 | + ``` |
| 114 | + |
| 115 | +## Spacing |
| 116 | + - Always include a single space in your self-closing tag. |
| 117 | + ```javascript |
| 118 | + // bad |
| 119 | + <Foo/> |
| 120 | +
|
| 121 | + // very bad |
| 122 | + <Foo /> |
| 123 | +
|
| 124 | + // bad |
| 125 | + <Foo |
| 126 | + /> |
| 127 | +
|
| 128 | + // good |
| 129 | + <Foo /> |
| 130 | + ``` |
| 131 | + |
| 132 | +## Props |
| 133 | + - Alphabetically order your props. |
| 134 | + ```javascript |
| 135 | + // bad |
| 136 | + <Foo |
| 137 | + className="bar baz" |
| 138 | + bar="bar" |
| 139 | + /> |
| 140 | +
|
| 141 | + // good |
| 142 | + <Foo |
| 143 | + bar="bar" |
| 144 | + className="bar baz" |
| 145 | + /> |
| 146 | + ``` |
| 147 | + |
| 148 | +## Parentheses |
| 149 | + - Wrap JSX tags in parentheses when they span more than one line: |
| 150 | + ```javascript |
| 151 | + /// bad |
| 152 | + render() { |
| 153 | + return <MyComponent className="long body" foo="bar" > |
| 154 | + <MyChild /> |
| 155 | + </MyComponent>; |
| 156 | + } |
| 157 | +
|
| 158 | + // good |
| 159 | + render() { |
| 160 | + return ( |
| 161 | + <MyComponent className="long body" foo="bar"> |
| 162 | + <MyChild /> |
| 163 | + </MyComponent> |
| 164 | + ); |
| 165 | + } |
| 166 | +
|
| 167 | + // good, when single line |
| 168 | + render() { |
| 169 | + const body = <div>hello</div>; |
| 170 | + return <MyComponent>{body}</MyComponent>; |
| 171 | + } |
| 172 | + ``` |
| 173 | + |
| 174 | +## Tags |
| 175 | + - Always self-close tags that have no children. |
| 176 | + ```javascript |
| 177 | + // bad |
| 178 | + <Foo className="stuff"></Foo> |
| 179 | +
|
| 180 | + // good |
| 181 | + <Foo className="stuff" /> |
| 182 | + ``` |
| 183 | + |
| 184 | + - If your component has multi-line properties, close its tag on a new line. |
| 185 | + ```javascript |
| 186 | + // bad |
| 187 | + <Foo |
| 188 | + bar="bar" |
| 189 | + baz="baz" /> |
| 190 | +
|
| 191 | + // good |
| 192 | + <Foo |
| 193 | + bar="bar" |
| 194 | + baz="baz" |
| 195 | + /> |
| 196 | + ``` |
| 197 | + |
| 198 | +## Ordering |
| 199 | + - Always follow the following order for methods in a react component: |
| 200 | + |
| 201 | + 1. displayName |
| 202 | + 2. mixins (as of React v0.13 mixins are deprecated) |
| 203 | + 3. statics |
| 204 | + 4. propTypes |
| 205 | + 5. getDefaultProps |
| 206 | + 6. getInitialState |
| 207 | + 7. componentWillMount |
| 208 | + 8. componentDidMount |
| 209 | + 9. componentWillReceiveProps |
| 210 | + 10. shouldComponentUpdate |
| 211 | + 11. componentWillUpdate |
| 212 | + 12. componentWillUnmount |
| 213 | + 13. *clickHandlers or eventHandlers* like onClickSubmit() or onChangeDescription() |
| 214 | + 14. *gettter methods for render* like getSelectReason() or getFooterContent() |
| 215 | + 15. *Optional render methods* like renderNavigation() or renderProfilePicture() |
| 216 | + 16. render |
| 217 | + |
| 218 | +**[⬆ back to top](#table-of-contents)** |
0 commit comments