|
27 | 27 |
|
28 | 28 | ## Class vs `React.createClass` vs stateless
|
29 | 29 |
|
30 |
| - - If you have internal state and/or refs, prefer `class extends React.Component` over `React.createClass` unless you have a very good reason to use mixins. |
31 |
| - |
32 |
| - eslint rules: [`react/prefer-es6-class`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md). |
| 30 | + - If you have internal state and/or refs, prefer `class extends React.Component` over `React.createClass` unless you have a very good reason to use mixins. [`react/prefer-es6-class`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md) |
33 | 31 |
|
34 | 32 | ```javascript
|
35 | 33 | // bad
|
|
48 | 46 | }
|
49 | 47 | }
|
50 | 48 | ```
|
51 |
| - |
| 49 | + |
52 | 50 | And if you don't have state or refs, prefer functions over classes:
|
53 |
| - |
| 51 | +
|
54 | 52 | ```javascript
|
55 | 53 |
|
56 | 54 | // bad
|
|
59 | 57 | return <div>{this.props.hello}</div>;
|
60 | 58 | }
|
61 | 59 | }
|
62 |
| - |
| 60 | +
|
63 | 61 | // good
|
64 | 62 | function Listing({ hello }) {
|
65 | 63 | return <div>{hello}</div>;
|
|
70 | 68 |
|
71 | 69 | - **Extensions**: Use `.jsx` extension for React components.
|
72 | 70 | - **Filename**: Use PascalCase for filenames. E.g., `ReservationCard.jsx`.
|
73 |
| - - **Reference Naming**: Use PascalCase for React components and camelCase for their instances. |
74 |
| -
|
75 |
| - eslint rules: [`react/jsx-pascal-case`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md). |
| 71 | + - **Reference Naming**: Use PascalCase for React components and camelCase for their instances. [`react/jsx-pascal-case`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md) |
76 | 72 |
|
77 | 73 | ```javascript
|
78 | 74 | // bad
|
|
119 | 115 |
|
120 | 116 | ## Alignment
|
121 | 117 |
|
122 |
| - - Follow these alignment styles for JSX syntax |
123 |
| -
|
124 |
| - eslint rules: [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md). |
| 118 | + - Follow these alignment styles for JSX syntax. [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md) |
125 | 119 |
|
126 | 120 | ```javascript
|
127 | 121 | // bad
|
|
148 | 142 |
|
149 | 143 | ## Quotes
|
150 | 144 |
|
151 |
| - - Always use double quotes (`"`) for JSX attributes, but single quotes for all other JS. |
| 145 | + - Always use double quotes (`"`) for JSX attributes, but single quotes for all other JS. [`jsx-quotes`](http://eslint.org/docs/rules/jsx-quotes) |
152 | 146 |
|
153 | 147 | > Why? JSX attributes [can't contain escaped quotes](http://eslint.org/docs/rules/jsx-quotes), so double quotes make conjunctions like `"don't"` easier to type.
|
154 | 148 | > Regular HTML attributes also typically use double quotes instead of single, so JSX attributes mirror this convention.
|
155 | 149 |
|
156 |
| - eslint rules: [`jsx-quotes`](http://eslint.org/docs/rules/jsx-quotes). |
157 |
| - |
158 | 150 | ```javascript
|
159 | 151 | // bad
|
160 | 152 | <Foo bar='bar' />
|
|
206 | 198 | />
|
207 | 199 | ```
|
208 | 200 |
|
209 |
| - - Omit the value of the prop when it is explicitly `true`. |
210 |
| - |
211 |
| - eslint rules: [`react/jsx-boolean-value`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md). |
| 201 | + - Omit the value of the prop when it is explicitly `true`. [`react/jsx-boolean-value`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md) |
212 | 202 |
|
213 | 203 | ```javascript
|
214 | 204 | // bad
|
|
224 | 214 |
|
225 | 215 | ## Parentheses
|
226 | 216 |
|
227 |
| - - Wrap JSX tags in parentheses when they span more than one line. |
228 |
| - |
229 |
| - eslint rules: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md). |
| 217 | + - Wrap JSX tags in parentheses when they span more than one line. [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md) |
230 | 218 |
|
231 | 219 | ```javascript
|
232 | 220 | // bad
|
|
254 | 242 |
|
255 | 243 | ## Tags
|
256 | 244 |
|
257 |
| - - Always self-close tags that have no children. |
258 |
| - |
259 |
| - eslint rules: [`react/self-closing-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md). |
| 245 | + - Always self-close tags that have no children. [`react/self-closing-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md) |
260 | 246 |
|
261 | 247 | ```javascript
|
262 | 248 | // bad
|
|
266 | 252 | <Foo className="stuff" />
|
267 | 253 | ```
|
268 | 254 |
|
269 |
| - - If your component has multi-line properties, close its tag on a new line. |
270 |
| - |
271 |
| - eslint rules: [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md). |
| 255 | + - If your component has multi-line properties, close its tag on a new line. [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md) |
272 | 256 |
|
273 | 257 | ```javascript
|
274 | 258 | // bad
|
|
285 | 269 |
|
286 | 270 | ## Methods
|
287 | 271 |
|
288 |
| - - Bind event handlers for the render method in the constructor. |
| 272 | + - Bind event handlers for the render method in the constructor. [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md) |
289 | 273 |
|
290 | 274 | > Why? A bind call in the render path creates a brand new function on every single render.
|
291 | 275 |
|
292 |
| - eslint rules: [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md). |
293 |
| - |
294 | 276 | ```javascript
|
295 | 277 | // bad
|
296 | 278 | class extends React.Component {
|
|
393 | 375 | export default Link;
|
394 | 376 | ```
|
395 | 377 |
|
396 |
| - - Ordering for `React.createClass`: |
| 378 | + - Ordering for `React.createClass`: [`react/sort-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md) |
397 | 379 |
|
398 | 380 | 1. `displayName`
|
399 | 381 | 1. `propTypes`
|
|
417 | 399 | 1. *Optional render methods* like `renderNavigation()` or `renderProfilePicture()`
|
418 | 400 | 1. `render`
|
419 | 401 |
|
420 |
| - eslint rules: [`react/sort-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md). |
421 |
| - |
422 | 402 | ## `isMounted`
|
423 | 403 |
|
424 |
| - - Do not use `isMounted`. |
| 404 | + - Do not use `isMounted`. [`react/no-is-mounted`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md) |
425 | 405 |
|
426 | 406 | > Why? [`isMounted` is an anti-pattern][anti-pattern], is not available when using ES6 classes, and is on its way to being officially deprecated.
|
427 | 407 |
|
428 | 408 | [anti-pattern]: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
|
429 | 409 |
|
430 |
| - eslint rules: [`react/no-is-mounted`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md). |
431 |
| - |
432 | 410 | **[⬆ back to top](#table-of-contents)**
|
0 commit comments