eser 2.1.9 → 3.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/deno.json +6 -0
- package/deno.lock +41 -0
- package/hizli-api.js +90 -0
- package/main.js +5 -0
- package/mod.js +3 -0
- package/mod_test.js +5 -0
- package/package.json +8 -37
- package/.editorconfig +0 -14
- package/.gitattributes +0 -11
- package/.github/FUNDING.yml +0 -5
- package/LICENSE +0 -22
- package/README.md +0 -3794
- package/css-in-javascript/README.md +0 -432
- package/linters/.eslintrc +0 -6
- package/linters/.markdownlint.json +0 -154
- package/packages/eslint-config-eser/.editorconfig +0 -14
- package/packages/eslint-config-eser/.eslintrc +0 -3
- package/packages/eslint-config-eser/README.md +0 -19
- package/packages/eslint-config-eser/index.js +0 -20
- package/packages/eslint-config-eser/package.json +0 -49
- package/packages/eslint-config-eser/rules/best-practices.js +0 -381
- package/packages/eslint-config-eser/rules/errors.js +0 -146
- package/packages/eslint-config-eser/rules/es6.js +0 -203
- package/packages/eslint-config-eser/rules/imports.js +0 -291
- package/packages/eslint-config-eser/rules/node.js +0 -43
- package/packages/eslint-config-eser/rules/strict.js +0 -5
- package/packages/eslint-config-eser/rules/style.js +0 -597
- package/packages/eslint-config-eser/rules/variables.js +0 -53
- package/packages/eslint-config-eser-react/.editorconfig +0 -14
- package/packages/eslint-config-eser-react/.eslintrc +0 -3
- package/packages/eslint-config-eser-react/README.md +0 -19
- package/packages/eslint-config-eser-react/index.js +0 -11
- package/packages/eslint-config-eser-react/package.json +0 -59
- package/packages/eslint-config-eser-react/rules/react-a11y.js +0 -275
- package/packages/eslint-config-eser-react/rules/react-hooks.js +0 -21
- package/packages/eslint-config-eser-react/rules/react.js +0 -600
- package/react/README.md +0 -717
package/react/README.md
DELETED
|
@@ -1,717 +0,0 @@
|
|
|
1
|
-
# [Eser: React/JSX Style Guide](https://github.com/eserozvataf/eser)
|
|
2
|
-
|
|
3
|
-
*A mostly reasonable approach to React and JSX*
|
|
4
|
-
|
|
5
|
-
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.
|
|
6
|
-
|
|
7
|
-
## Table of Contents
|
|
8
|
-
|
|
9
|
-
1. [Basic Rules](#basic-rules)
|
|
10
|
-
1. [Class vs `React.createClass` vs stateless](#class-vs-reactcreateclass-vs-stateless)
|
|
11
|
-
1. [Mixins](#mixins)
|
|
12
|
-
1. [Naming](#naming)
|
|
13
|
-
1. [Declaration](#declaration)
|
|
14
|
-
1. [Alignment](#alignment)
|
|
15
|
-
1. [Quotes](#quotes)
|
|
16
|
-
1. [Spacing](#spacing)
|
|
17
|
-
1. [Props](#props)
|
|
18
|
-
1. [Refs](#refs)
|
|
19
|
-
1. [Parentheses](#parentheses)
|
|
20
|
-
1. [Tags](#tags)
|
|
21
|
-
1. [Methods](#methods)
|
|
22
|
-
1. [Ordering](#ordering)
|
|
23
|
-
1. [`isMounted`](#ismounted)
|
|
24
|
-
|
|
25
|
-
## Basic Rules
|
|
26
|
-
|
|
27
|
-
- Only include one React component per file.
|
|
28
|
-
- However, multiple [Stateless, or Pure, Components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions) are allowed per file. eslint: [`react/no-multi-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md#ignorestateless).
|
|
29
|
-
- Always use JSX syntax.
|
|
30
|
-
- Do not use `React.createElement` unless you’re initializing the app from a file that is not JSX.
|
|
31
|
-
- [`react/forbid-prop-types`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-prop-types.md) will allow `arrays` and `objects` only if it is explicitly noted what `array` and `object` contains, using `arrayOf`, `objectOf`, or `shape`.
|
|
32
|
-
|
|
33
|
-
## Class vs `React.createClass` vs stateless
|
|
34
|
-
|
|
35
|
-
- If you have internal state and/or refs, prefer `class extends React.Component` over `React.createClass`. eslint: [`react/prefer-es6-class`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md) [`react/prefer-stateless-function`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-stateless-function.md)
|
|
36
|
-
|
|
37
|
-
```jsx
|
|
38
|
-
// bad
|
|
39
|
-
const Listing = React.createClass({
|
|
40
|
-
// ...
|
|
41
|
-
render() {
|
|
42
|
-
return <div>{this.state.hello}</div>;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// good
|
|
47
|
-
class Listing extends React.Component {
|
|
48
|
-
// ...
|
|
49
|
-
render() {
|
|
50
|
-
return <div>{this.state.hello}</div>;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
And if you don’t have state or refs, prefer normal functions (not arrow functions) over classes:
|
|
56
|
-
|
|
57
|
-
```jsx
|
|
58
|
-
// bad
|
|
59
|
-
class Listing extends React.Component {
|
|
60
|
-
render() {
|
|
61
|
-
return <div>{this.props.hello}</div>;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// bad (relying on function name inference is discouraged)
|
|
66
|
-
const Listing = ({ hello }) => (
|
|
67
|
-
<div>{hello}</div>
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
// good
|
|
71
|
-
function Listing({ hello }) {
|
|
72
|
-
return <div>{hello}</div>;
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Mixins
|
|
77
|
-
|
|
78
|
-
- [Do not use mixins](https://facebook.github.io/react/blog/2016/07/13/mixins-considered-harmful.html).
|
|
79
|
-
|
|
80
|
-
> 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.
|
|
81
|
-
|
|
82
|
-
## Naming
|
|
83
|
-
|
|
84
|
-
- **Extensions**: Use `.jsx` extension for React components. eslint: [`react/jsx-filename-extension`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-filename-extension.md)
|
|
85
|
-
- **Filename**: Use PascalCase for filenames. E.g., `ReservationCard.jsx`.
|
|
86
|
-
- **Reference Naming**: Use PascalCase for React components and camelCase for their instances. eslint: [`react/jsx-pascal-case`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md)
|
|
87
|
-
|
|
88
|
-
```jsx
|
|
89
|
-
// bad
|
|
90
|
-
import reservationCard from './ReservationCard';
|
|
91
|
-
|
|
92
|
-
// good
|
|
93
|
-
import ReservationCard from './ReservationCard';
|
|
94
|
-
|
|
95
|
-
// bad
|
|
96
|
-
const ReservationItem = <ReservationCard />;
|
|
97
|
-
|
|
98
|
-
// good
|
|
99
|
-
const reservationItem = <ReservationCard />;
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
- **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:
|
|
103
|
-
|
|
104
|
-
```jsx
|
|
105
|
-
// bad
|
|
106
|
-
import Footer from './Footer/Footer';
|
|
107
|
-
|
|
108
|
-
// bad
|
|
109
|
-
import Footer from './Footer/index';
|
|
110
|
-
|
|
111
|
-
// good
|
|
112
|
-
import Footer from './Footer';
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
- **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)`.
|
|
116
|
-
|
|
117
|
-
> 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.
|
|
118
|
-
|
|
119
|
-
```jsx
|
|
120
|
-
// bad
|
|
121
|
-
export default function withFoo(WrappedComponent) {
|
|
122
|
-
return function WithFoo(props) {
|
|
123
|
-
return <WrappedComponent {...props} foo />;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// good
|
|
128
|
-
export default function withFoo(WrappedComponent) {
|
|
129
|
-
function WithFoo(props) {
|
|
130
|
-
return <WrappedComponent {...props} foo />;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const wrappedComponentName = WrappedComponent.displayName
|
|
134
|
-
|| WrappedComponent.name
|
|
135
|
-
|| 'Component';
|
|
136
|
-
|
|
137
|
-
WithFoo.displayName = `withFoo(${wrappedComponentName})`;
|
|
138
|
-
return WithFoo;
|
|
139
|
-
}
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
- **Props Naming**: Avoid using DOM component prop names for different purposes.
|
|
143
|
-
|
|
144
|
-
> 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.
|
|
145
|
-
|
|
146
|
-
```jsx
|
|
147
|
-
// bad
|
|
148
|
-
<MyComponent style="fancy" />
|
|
149
|
-
|
|
150
|
-
// bad
|
|
151
|
-
<MyComponent className="fancy" />
|
|
152
|
-
|
|
153
|
-
// good
|
|
154
|
-
<MyComponent variant="fancy" />
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
## Declaration
|
|
158
|
-
|
|
159
|
-
- Do not use `displayName` for naming components. Instead, name the component by reference.
|
|
160
|
-
|
|
161
|
-
```jsx
|
|
162
|
-
// bad
|
|
163
|
-
export default React.createClass({
|
|
164
|
-
displayName: 'ReservationCard',
|
|
165
|
-
// stuff goes here
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
// good
|
|
169
|
-
export default class ReservationCard extends React.Component {
|
|
170
|
-
}
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## Alignment
|
|
174
|
-
|
|
175
|
-
- Follow these alignment styles for JSX syntax. eslint: [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md) [`react/jsx-closing-tag-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-tag-location.md)
|
|
176
|
-
|
|
177
|
-
```jsx
|
|
178
|
-
// bad
|
|
179
|
-
<Foo superLongParam="bar"
|
|
180
|
-
anotherSuperLongParam="baz" />
|
|
181
|
-
|
|
182
|
-
// good
|
|
183
|
-
<Foo
|
|
184
|
-
superLongParam="bar"
|
|
185
|
-
anotherSuperLongParam="baz"
|
|
186
|
-
/>
|
|
187
|
-
|
|
188
|
-
// if props fit in one line then keep it on the same line
|
|
189
|
-
<Foo bar="bar" />
|
|
190
|
-
|
|
191
|
-
// children get indented normally
|
|
192
|
-
<Foo
|
|
193
|
-
superLongParam="bar"
|
|
194
|
-
anotherSuperLongParam="baz"
|
|
195
|
-
>
|
|
196
|
-
<Quux />
|
|
197
|
-
</Foo>
|
|
198
|
-
|
|
199
|
-
// bad
|
|
200
|
-
{showButton &&
|
|
201
|
-
<Button />
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// bad
|
|
205
|
-
{
|
|
206
|
-
showButton &&
|
|
207
|
-
<Button />
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// good
|
|
211
|
-
{showButton && (
|
|
212
|
-
<Button />
|
|
213
|
-
)}
|
|
214
|
-
|
|
215
|
-
// good
|
|
216
|
-
{showButton && <Button />}
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
## Quotes
|
|
220
|
-
|
|
221
|
-
- Always use double quotes (`"`) for JSX attributes, but single quotes (`'`) for all other JS. eslint: [`jsx-quotes`](https://eslint.org/docs/rules/jsx-quotes)
|
|
222
|
-
|
|
223
|
-
> Why? Regular HTML attributes also typically use double quotes instead of single, so JSX attributes mirror this convention.
|
|
224
|
-
|
|
225
|
-
```jsx
|
|
226
|
-
// bad
|
|
227
|
-
<Foo bar='bar' />
|
|
228
|
-
|
|
229
|
-
// good
|
|
230
|
-
<Foo bar="bar" />
|
|
231
|
-
|
|
232
|
-
// bad
|
|
233
|
-
<Foo style={{ left: "20px" }} />
|
|
234
|
-
|
|
235
|
-
// good
|
|
236
|
-
<Foo style={{ left: '20px' }} />
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
## Spacing
|
|
240
|
-
|
|
241
|
-
- Always include a single space in your self-closing tag. eslint: [`no-multi-spaces`](https://eslint.org/docs/rules/no-multi-spaces), [`react/jsx-tag-spacing`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-tag-spacing.md)
|
|
242
|
-
|
|
243
|
-
```jsx
|
|
244
|
-
// bad
|
|
245
|
-
<Foo/>
|
|
246
|
-
|
|
247
|
-
// very bad
|
|
248
|
-
<Foo />
|
|
249
|
-
|
|
250
|
-
// bad
|
|
251
|
-
<Foo
|
|
252
|
-
/>
|
|
253
|
-
|
|
254
|
-
// good
|
|
255
|
-
<Foo />
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
- Do not pad JSX curly braces with spaces. eslint: [`react/jsx-curly-spacing`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-spacing.md)
|
|
259
|
-
|
|
260
|
-
```jsx
|
|
261
|
-
// bad
|
|
262
|
-
<Foo bar={ baz } />
|
|
263
|
-
|
|
264
|
-
// good
|
|
265
|
-
<Foo bar={baz} />
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
## Props
|
|
269
|
-
|
|
270
|
-
- Always use camelCase for prop names.
|
|
271
|
-
|
|
272
|
-
```jsx
|
|
273
|
-
// bad
|
|
274
|
-
<Foo
|
|
275
|
-
UserName="hello"
|
|
276
|
-
phone_number={12345678}
|
|
277
|
-
/>
|
|
278
|
-
|
|
279
|
-
// good
|
|
280
|
-
<Foo
|
|
281
|
-
userName="hello"
|
|
282
|
-
phoneNumber={12345678}
|
|
283
|
-
/>
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
- Omit the value of the prop when it is explicitly `true`. eslint: [`react/jsx-boolean-value`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md)
|
|
287
|
-
|
|
288
|
-
```jsx
|
|
289
|
-
// bad
|
|
290
|
-
<Foo
|
|
291
|
-
hidden={true}
|
|
292
|
-
/>
|
|
293
|
-
|
|
294
|
-
// good
|
|
295
|
-
<Foo
|
|
296
|
-
hidden
|
|
297
|
-
/>
|
|
298
|
-
|
|
299
|
-
// good
|
|
300
|
-
<Foo hidden />
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
- 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`](https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/alt-text.md)
|
|
304
|
-
|
|
305
|
-
```jsx
|
|
306
|
-
// bad
|
|
307
|
-
<img src="hello.jpg" />
|
|
308
|
-
|
|
309
|
-
// good
|
|
310
|
-
<img src="hello.jpg" alt="Me waving hello" />
|
|
311
|
-
|
|
312
|
-
// good
|
|
313
|
-
<img src="hello.jpg" alt="" />
|
|
314
|
-
|
|
315
|
-
// good
|
|
316
|
-
<img src="hello.jpg" role="presentation" />
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
- Do not use words like "image", "photo", or "picture" in `<img>` `alt` props. eslint: [`jsx-a11y/img-redundant-alt`](https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/img-redundant-alt.md)
|
|
320
|
-
|
|
321
|
-
> Why? Screenreaders already announce `img` elements as images, so there is no need to include this information in the alt text.
|
|
322
|
-
|
|
323
|
-
```jsx
|
|
324
|
-
// bad
|
|
325
|
-
<img src="hello.jpg" alt="Picture of me waving hello" />
|
|
326
|
-
|
|
327
|
-
// good
|
|
328
|
-
<img src="hello.jpg" alt="Me waving hello" />
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
- Use only valid, non-abstract [ARIA roles](https://www.w3.org/TR/wai-aria/#usage_intro). eslint: [`jsx-a11y/aria-role`](https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-role.md)
|
|
332
|
-
|
|
333
|
-
```jsx
|
|
334
|
-
// bad - not an ARIA role
|
|
335
|
-
<div role="datepicker" />
|
|
336
|
-
|
|
337
|
-
// bad - abstract ARIA role
|
|
338
|
-
<div role="range" />
|
|
339
|
-
|
|
340
|
-
// good
|
|
341
|
-
<div role="button" />
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
- Do not use `accessKey` on elements. eslint: [`jsx-a11y/no-access-key`](https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-access-key.md)
|
|
345
|
-
|
|
346
|
-
> Why? Inconsistencies between keyboard shortcuts and keyboard commands used by people using screenreaders and keyboards complicate accessibility.
|
|
347
|
-
|
|
348
|
-
```jsx
|
|
349
|
-
// bad
|
|
350
|
-
<div accessKey="h" />
|
|
351
|
-
|
|
352
|
-
// good
|
|
353
|
-
<div />
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
- Avoid using an array index as `key` prop, prefer a stable ID. eslint: [`react/no-array-index-key`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md)
|
|
357
|
-
|
|
358
|
-
> Why? Not using a stable ID [is an anti-pattern](https://medium.com/@robinpokorny/index-as-a-key-is-an-anti-pattern-e0349aece318) because it can negatively impact performance and cause issues with component state.
|
|
359
|
-
|
|
360
|
-
We don’t recommend using indexes for keys if the order of items may change.
|
|
361
|
-
|
|
362
|
-
```jsx
|
|
363
|
-
// bad
|
|
364
|
-
{todos.map((todo, index) =>
|
|
365
|
-
<Todo
|
|
366
|
-
{...todo}
|
|
367
|
-
key={index}
|
|
368
|
-
/>
|
|
369
|
-
)}
|
|
370
|
-
|
|
371
|
-
// good
|
|
372
|
-
{todos.map(todo => (
|
|
373
|
-
<Todo
|
|
374
|
-
{...todo}
|
|
375
|
-
key={todo.id}
|
|
376
|
-
/>
|
|
377
|
-
))}
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
- Always define explicit defaultProps for all non-required props.
|
|
381
|
-
|
|
382
|
-
> 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.
|
|
383
|
-
|
|
384
|
-
```jsx
|
|
385
|
-
// bad
|
|
386
|
-
function SFC({ foo, bar, children }) {
|
|
387
|
-
return <div>{foo}{bar}{children}</div>;
|
|
388
|
-
}
|
|
389
|
-
SFC.propTypes = {
|
|
390
|
-
foo: PropTypes.number.isRequired,
|
|
391
|
-
bar: PropTypes.string,
|
|
392
|
-
children: PropTypes.node,
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
// good
|
|
396
|
-
function SFC({ foo, bar, children }) {
|
|
397
|
-
return <div>{foo}{bar}{children}</div>;
|
|
398
|
-
}
|
|
399
|
-
SFC.propTypes = {
|
|
400
|
-
foo: PropTypes.number.isRequired,
|
|
401
|
-
bar: PropTypes.string,
|
|
402
|
-
children: PropTypes.node,
|
|
403
|
-
};
|
|
404
|
-
SFC.defaultProps = {
|
|
405
|
-
bar: '',
|
|
406
|
-
children: null,
|
|
407
|
-
};
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
- Use spread props sparingly.
|
|
411
|
-
> 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](https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html).
|
|
412
|
-
|
|
413
|
-
Exceptions:
|
|
414
|
-
|
|
415
|
-
- HOCs that proxy down props and hoist propTypes
|
|
416
|
-
|
|
417
|
-
```jsx
|
|
418
|
-
function HOC(WrappedComponent) {
|
|
419
|
-
return class Proxy extends React.Component {
|
|
420
|
-
Proxy.propTypes = {
|
|
421
|
-
text: PropTypes.string,
|
|
422
|
-
isLoading: PropTypes.bool
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
render() {
|
|
426
|
-
return <WrappedComponent {...this.props} />
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
```
|
|
431
|
-
|
|
432
|
-
- Spreading objects with known, explicit props. This can be particularly useful when testing React components with Mocha’s beforeEach construct.
|
|
433
|
-
|
|
434
|
-
```jsx
|
|
435
|
-
export default function Foo {
|
|
436
|
-
const props = {
|
|
437
|
-
text: '',
|
|
438
|
-
isPublished: false
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return (<div {...props} />);
|
|
442
|
-
}
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
Notes for use:
|
|
446
|
-
Filter out unnecessary props when possible. Also, use [prop-types-exact](https://www.npmjs.com/package/prop-types-exact) to help prevent bugs.
|
|
447
|
-
|
|
448
|
-
```jsx
|
|
449
|
-
// bad
|
|
450
|
-
render() {
|
|
451
|
-
const { irrelevantProp, ...relevantProps } = this.props;
|
|
452
|
-
return <WrappedComponent {...this.props} />
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
// good
|
|
456
|
-
render() {
|
|
457
|
-
const { irrelevantProp, ...relevantProps } = this.props;
|
|
458
|
-
return <WrappedComponent {...relevantProps} />
|
|
459
|
-
}
|
|
460
|
-
```
|
|
461
|
-
|
|
462
|
-
## Refs
|
|
463
|
-
|
|
464
|
-
- Always use ref callbacks. eslint: [`react/no-string-refs`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)
|
|
465
|
-
|
|
466
|
-
```jsx
|
|
467
|
-
// bad
|
|
468
|
-
<Foo
|
|
469
|
-
ref="myRef"
|
|
470
|
-
/>
|
|
471
|
-
|
|
472
|
-
// good
|
|
473
|
-
<Foo
|
|
474
|
-
ref={(ref) => { this.myRef = ref; }}
|
|
475
|
-
/>
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
## Parentheses
|
|
479
|
-
|
|
480
|
-
- Wrap JSX tags in parentheses when they span more than one line. eslint: [`react/jsx-wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-wrap-multilines.md)
|
|
481
|
-
|
|
482
|
-
```jsx
|
|
483
|
-
// bad
|
|
484
|
-
render() {
|
|
485
|
-
return <MyComponent variant="long body" foo="bar">
|
|
486
|
-
<MyChild />
|
|
487
|
-
</MyComponent>;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// good
|
|
491
|
-
render() {
|
|
492
|
-
return (
|
|
493
|
-
<MyComponent variant="long body" foo="bar">
|
|
494
|
-
<MyChild />
|
|
495
|
-
</MyComponent>
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// good, when single line
|
|
500
|
-
render() {
|
|
501
|
-
const body = <div>hello</div>;
|
|
502
|
-
return <MyComponent>{body}</MyComponent>;
|
|
503
|
-
}
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
## Tags
|
|
507
|
-
|
|
508
|
-
- Always self-close tags that have no children. eslint: [`react/self-closing-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md)
|
|
509
|
-
|
|
510
|
-
```jsx
|
|
511
|
-
// bad
|
|
512
|
-
<Foo variant="stuff"></Foo>
|
|
513
|
-
|
|
514
|
-
// good
|
|
515
|
-
<Foo variant="stuff" />
|
|
516
|
-
```
|
|
517
|
-
|
|
518
|
-
- If your component has multi-line properties, close its tag on a new line. eslint: [`react/jsx-closing-bracket-location`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md)
|
|
519
|
-
|
|
520
|
-
```jsx
|
|
521
|
-
// bad
|
|
522
|
-
<Foo
|
|
523
|
-
bar="bar"
|
|
524
|
-
baz="baz" />
|
|
525
|
-
|
|
526
|
-
// good
|
|
527
|
-
<Foo
|
|
528
|
-
bar="bar"
|
|
529
|
-
baz="baz"
|
|
530
|
-
/>
|
|
531
|
-
```
|
|
532
|
-
|
|
533
|
-
## Methods
|
|
534
|
-
|
|
535
|
-
- 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 performance](https://www.bignerdranch.com/blog/choosing-the-best-approach-for-react-event-handlers/), in particular when passed to custom components that might be PureComponents, because they will trigger a possibly needless rerender every time.
|
|
536
|
-
|
|
537
|
-
```jsx
|
|
538
|
-
function ItemList(props) {
|
|
539
|
-
return (
|
|
540
|
-
<ul>
|
|
541
|
-
{props.items.map((item, index) => (
|
|
542
|
-
<Item
|
|
543
|
-
key={item.key}
|
|
544
|
-
onClick={(event) => { doSomethingWith(event, item.name, index); }}
|
|
545
|
-
/>
|
|
546
|
-
))}
|
|
547
|
-
</ul>
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
```
|
|
551
|
-
|
|
552
|
-
- Bind event handlers for the render method in the constructor. eslint: [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md)
|
|
553
|
-
|
|
554
|
-
> 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 performance](https://medium.com/@charpeni/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1), and because conceptually, class fields are for data, not logic.
|
|
555
|
-
|
|
556
|
-
```jsx
|
|
557
|
-
// bad
|
|
558
|
-
class extends React.Component {
|
|
559
|
-
onClickDiv() {
|
|
560
|
-
// do stuff
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
render() {
|
|
564
|
-
return <div onClick={this.onClickDiv.bind(this)} />;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
// very bad
|
|
569
|
-
class extends React.Component {
|
|
570
|
-
onClickDiv = () => {
|
|
571
|
-
// do stuff
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
render() {
|
|
575
|
-
return <div onClick={this.onClickDiv} />
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// good
|
|
580
|
-
class extends React.Component {
|
|
581
|
-
constructor(props) {
|
|
582
|
-
super(props);
|
|
583
|
-
|
|
584
|
-
this.onClickDiv = this.onClickDiv.bind(this);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
onClickDiv() {
|
|
588
|
-
// do stuff
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
render() {
|
|
592
|
-
return <div onClick={this.onClickDiv} />;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
- Do not use underscore prefix for internal methods of a React component.
|
|
598
|
-
> 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 [#1024](https://github.com/airbnb/javascript/issues/1024), and [#490](https://github.com/airbnb/javascript/issues/490) for a more in-depth discussion.
|
|
599
|
-
|
|
600
|
-
```jsx
|
|
601
|
-
// bad
|
|
602
|
-
React.createClass({
|
|
603
|
-
_onClickSubmit() {
|
|
604
|
-
// do stuff
|
|
605
|
-
},
|
|
606
|
-
|
|
607
|
-
// other stuff
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
// good
|
|
611
|
-
class extends React.Component {
|
|
612
|
-
onClickSubmit() {
|
|
613
|
-
// do stuff
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
// other stuff
|
|
617
|
-
}
|
|
618
|
-
```
|
|
619
|
-
|
|
620
|
-
- Be sure to return a value in your `render` methods. eslint: [`react/require-render-return`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-render-return.md)
|
|
621
|
-
|
|
622
|
-
```jsx
|
|
623
|
-
// bad
|
|
624
|
-
render() {
|
|
625
|
-
(<div />);
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
// good
|
|
629
|
-
render() {
|
|
630
|
-
return (<div />);
|
|
631
|
-
}
|
|
632
|
-
```
|
|
633
|
-
|
|
634
|
-
## Ordering
|
|
635
|
-
|
|
636
|
-
- Ordering for `class extends React.Component`:
|
|
637
|
-
|
|
638
|
-
1. optional `static` methods
|
|
639
|
-
1. `constructor`
|
|
640
|
-
1. `getChildContext`
|
|
641
|
-
1. `componentWillMount`
|
|
642
|
-
1. `componentDidMount`
|
|
643
|
-
1. `componentWillReceiveProps`
|
|
644
|
-
1. `shouldComponentUpdate`
|
|
645
|
-
1. `componentWillUpdate`
|
|
646
|
-
1. `componentDidUpdate`
|
|
647
|
-
1. `componentWillUnmount`
|
|
648
|
-
1. *clickHandlers or eventHandlers* like `onClickSubmit()` or `onChangeDescription()`
|
|
649
|
-
1. *getter methods for `render`* like `getSelectReason()` or `getFooterContent()`
|
|
650
|
-
1. *optional render methods* like `renderNavigation()` or `renderProfilePicture()`
|
|
651
|
-
1. `render`
|
|
652
|
-
|
|
653
|
-
- How to define `propTypes`, `defaultProps`, `contextTypes`, etc...
|
|
654
|
-
|
|
655
|
-
```jsx
|
|
656
|
-
import React from 'react';
|
|
657
|
-
import PropTypes from 'prop-types';
|
|
658
|
-
|
|
659
|
-
const propTypes = {
|
|
660
|
-
id: PropTypes.number.isRequired,
|
|
661
|
-
url: PropTypes.string.isRequired,
|
|
662
|
-
text: PropTypes.string,
|
|
663
|
-
};
|
|
664
|
-
|
|
665
|
-
const defaultProps = {
|
|
666
|
-
text: 'Hello World',
|
|
667
|
-
};
|
|
668
|
-
|
|
669
|
-
class Link extends React.Component {
|
|
670
|
-
static methodsAreOk() {
|
|
671
|
-
return true;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
render() {
|
|
675
|
-
return <a href={this.props.url} data-id={this.props.id}>{this.props.text}</a>;
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
Link.propTypes = propTypes;
|
|
680
|
-
Link.defaultProps = defaultProps;
|
|
681
|
-
|
|
682
|
-
export default Link;
|
|
683
|
-
```
|
|
684
|
-
|
|
685
|
-
- Ordering for `React.createClass`: eslint: [`react/sort-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md)
|
|
686
|
-
|
|
687
|
-
1. `displayName`
|
|
688
|
-
1. `propTypes`
|
|
689
|
-
1. `contextTypes`
|
|
690
|
-
1. `childContextTypes`
|
|
691
|
-
1. `mixins`
|
|
692
|
-
1. `statics`
|
|
693
|
-
1. `defaultProps`
|
|
694
|
-
1. `getDefaultProps`
|
|
695
|
-
1. `getInitialState`
|
|
696
|
-
1. `getChildContext`
|
|
697
|
-
1. `componentWillMount`
|
|
698
|
-
1. `componentDidMount`
|
|
699
|
-
1. `componentWillReceiveProps`
|
|
700
|
-
1. `shouldComponentUpdate`
|
|
701
|
-
1. `componentWillUpdate`
|
|
702
|
-
1. `componentDidUpdate`
|
|
703
|
-
1. `componentWillUnmount`
|
|
704
|
-
1. *clickHandlers or eventHandlers* like `onClickSubmit()` or `onChangeDescription()`
|
|
705
|
-
1. *getter methods for `render`* like `getSelectReason()` or `getFooterContent()`
|
|
706
|
-
1. *optional render methods* like `renderNavigation()` or `renderProfilePicture()`
|
|
707
|
-
1. `render`
|
|
708
|
-
|
|
709
|
-
## `isMounted`
|
|
710
|
-
|
|
711
|
-
- Do not use `isMounted`. eslint: [`react/no-is-mounted`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md)
|
|
712
|
-
|
|
713
|
-
> Why? [`isMounted` is an anti-pattern][anti-pattern], is not available when using ES6 classes, and is on its way to being officially deprecated.
|
|
714
|
-
|
|
715
|
-
[anti-pattern]: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
|
|
716
|
-
|
|
717
|
-
**[⬆ back to top](#table-of-contents)**
|