@entva/styleguide 0.0.0 → 2.30.0
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/README.md +16 -77
- package/package.json +29 -6
- package/.github/workflows/ci.yml +0 -14
- package/css/README.md +0 -385
- package/css/packages/stylelint/LICENSE +0 -21
- package/css/packages/stylelint/README.md +0 -27
- package/css/packages/stylelint/eslint.config.js +0 -3
- package/css/packages/stylelint/index.js +0 -84
- package/css/packages/stylelint/package-lock.json +0 -4042
- package/css/packages/stylelint/package.json +0 -42
- package/html/README.md +0 -98
- package/javascript/README.md +0 -3255
- package/javascript/packages/eslint-base/LICENSE +0 -21
- package/javascript/packages/eslint-base/README.md +0 -27
- package/javascript/packages/eslint-base/eslint.config.js +0 -3
- package/javascript/packages/eslint-base/index.js +0 -1084
- package/javascript/packages/eslint-base/package-lock.json +0 -2653
- package/javascript/packages/eslint-base/package.json +0 -36
- package/javascript/packages/eslint-react/LICENSE +0 -21
- package/javascript/packages/eslint-react/README.md +0 -27
- package/javascript/packages/eslint-react/eslint.config.js +0 -3
- package/javascript/packages/eslint-react/index.js +0 -526
- package/javascript/packages/eslint-react/package-lock.json +0 -3035
- package/javascript/packages/eslint-react/package.json +0 -42
- package/react/README.md +0 -726
- package/scripts/for-each-package +0 -12
- package/test/css.js +0 -36
- package/test/index.js +0 -7
- package/test/js.js +0 -35
- package/test/utils.js +0 -63
- package/tooling/packages/biome/LICENSE +0 -21
- package/tooling/packages/biome/README.md +0 -27
- package/tooling/packages/biome/package-lock.json +0 -183
- package/tooling/packages/biome/package.json +0 -38
- package/tsconfig.json +0 -31
- package/typescript/README.md +0 -66
- package/typescript/packages/eslint-typescript-base/LICENSE +0 -21
- package/typescript/packages/eslint-typescript-base/README.md +0 -27
- package/typescript/packages/eslint-typescript-base/eslint.config.js +0 -3
- package/typescript/packages/eslint-typescript-base/index.js +0 -272
- package/typescript/packages/eslint-typescript-base/package-lock.json +0 -3215
- package/typescript/packages/eslint-typescript-base/package.json +0 -41
- package/typescript/packages/eslint-typescript-base/tsconfig.json +0 -30
- package/typescript/packages/eslint-typescript-react/LICENSE +0 -21
- package/typescript/packages/eslint-typescript-react/README.md +0 -27
- package/typescript/packages/eslint-typescript-react/eslint.config.js +0 -3
- package/typescript/packages/eslint-typescript-react/index.js +0 -75
- package/typescript/packages/eslint-typescript-react/package-lock.json +0 -3630
- package/typescript/packages/eslint-typescript-react/package.json +0 -41
- package/typescript/packages/eslint-typescript-react/tsconfig.json +0 -30
- /package/{tooling/packages/biome/biome.json → biome.json} +0 -0
package/react/README.md
DELETED
|
@@ -1,726 +0,0 @@
|
|
|
1
|
-
# React/JSX Style Guide
|
|
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. [Hooks vs Class vs `React.createClass` vs stateless](#hooks-vs-class-vs-reactcreateclass-vs-stateless)
|
|
11
|
-
1. [Mixins](#mixins)
|
|
12
|
-
1. [Naming](#naming)
|
|
13
|
-
1. [Alignment](#alignment)
|
|
14
|
-
1. [Quotes](#quotes)
|
|
15
|
-
1. [Spacing](#spacing)
|
|
16
|
-
1. [Props](#props)
|
|
17
|
-
1. [Refs](#refs)
|
|
18
|
-
1. [Parentheses](#parentheses)
|
|
19
|
-
1. [Tags](#tags)
|
|
20
|
-
1. [Methods](#methods)
|
|
21
|
-
1. [Ordering](#ordering)
|
|
22
|
-
1. [CSS Modules](#css-modules)
|
|
23
|
-
|
|
24
|
-
## Basic Rules
|
|
25
|
-
|
|
26
|
-
- Only include one React component per file.
|
|
27
|
-
- However, multiple simple [Functional](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions) sub-components 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).
|
|
28
|
-
- Always use JSX syntax.
|
|
29
|
-
- Do not use `React.createElement` unless you’re initializing the app from a file that is not JSX.
|
|
30
|
-
- [`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`.
|
|
31
|
-
|
|
32
|
-
## Hooks vs Class vs `React.createClass` vs stateless
|
|
33
|
-
|
|
34
|
-
- Prefer functional components with Hooks over `class extends React.Component` (when possible) or `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)
|
|
35
|
-
|
|
36
|
-
```jsx
|
|
37
|
-
// bad
|
|
38
|
-
const Listing = React.createClass({
|
|
39
|
-
// ...
|
|
40
|
-
render() {
|
|
41
|
-
return <div>{this.state.hello}</div>;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// better
|
|
46
|
-
class Listing extends React.Component {
|
|
47
|
-
// ...
|
|
48
|
-
render() {
|
|
49
|
-
return <div>{this.state.hello}</div>;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
And if you can accomplish the same with Hooks:
|
|
55
|
-
|
|
56
|
-
```jsx
|
|
57
|
-
// better
|
|
58
|
-
class Listing extends React.Component {
|
|
59
|
-
render() {
|
|
60
|
-
return <div>{this.props.hello}</div>;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// best
|
|
65
|
-
const Listing = ({ hello }) => (
|
|
66
|
-
<div>{hello}</div>
|
|
67
|
-
);
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## Mixins
|
|
71
|
-
|
|
72
|
-
- [Do not use mixins](https://facebook.github.io/react/blog/2016/07/13/mixins-considered-harmful.html).
|
|
73
|
-
|
|
74
|
-
> Why? It's a legacy feature not meant to be used in modern React applications.
|
|
75
|
-
|
|
76
|
-
## Naming
|
|
77
|
-
- **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)
|
|
78
|
-
- **Filename**: Use snake_case for filenames. E.g., `reservation_card.jsx`. Why? Many OS use non case sensitive file systems by default, renaming `Myfile` to `MyFile` in case of a typo might be difficult.
|
|
79
|
-
- **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)
|
|
80
|
-
|
|
81
|
-
```jsx
|
|
82
|
-
// bad
|
|
83
|
-
import reservationCard from './reservation_card';
|
|
84
|
-
|
|
85
|
-
// good
|
|
86
|
-
import ReservationCard from './reservation_card';
|
|
87
|
-
|
|
88
|
-
// bad
|
|
89
|
-
const ReservationItem = <ReservationCard />;
|
|
90
|
-
|
|
91
|
-
// good
|
|
92
|
-
const reservationItem = <ReservationCard />;
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
- **Component Naming**: Use the filename as the component name. For example, `reservation_card.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:
|
|
96
|
-
|
|
97
|
-
```jsx
|
|
98
|
-
// bad
|
|
99
|
-
import Footer from './footer/footer';
|
|
100
|
-
|
|
101
|
-
// bad
|
|
102
|
-
import Footer from './footer/index';
|
|
103
|
-
|
|
104
|
-
// good
|
|
105
|
-
import Footer from './footer';
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
- **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)`.
|
|
109
|
-
|
|
110
|
-
> 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.
|
|
111
|
-
|
|
112
|
-
```jsx
|
|
113
|
-
// bad
|
|
114
|
-
export default function withFoo(WrappedComponent) {
|
|
115
|
-
return function WithFoo(props) {
|
|
116
|
-
return <WrappedComponent {...props} foo />;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// good
|
|
121
|
-
export default function withFoo(WrappedComponent) {
|
|
122
|
-
function WithFoo(props) {
|
|
123
|
-
return <WrappedComponent {...props} foo />;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const wrappedComponentName = WrappedComponent.displayName
|
|
127
|
-
|| WrappedComponent.name
|
|
128
|
-
|| 'Component';
|
|
129
|
-
|
|
130
|
-
WithFoo.displayName = `withFoo(${wrappedComponentName})`;
|
|
131
|
-
return WithFoo;
|
|
132
|
-
}
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
- **Props Naming**: Avoid using DOM component prop names for different purposes.
|
|
136
|
-
|
|
137
|
-
> 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.
|
|
138
|
-
|
|
139
|
-
```jsx
|
|
140
|
-
// bad
|
|
141
|
-
<MyComponent style="fancy" />
|
|
142
|
-
|
|
143
|
-
// bad
|
|
144
|
-
<MyComponent className="fancy" />
|
|
145
|
-
|
|
146
|
-
// good
|
|
147
|
-
<MyComponent variant="fancy" />
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
## Alignment
|
|
151
|
-
|
|
152
|
-
- 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)
|
|
153
|
-
|
|
154
|
-
```jsx
|
|
155
|
-
// bad
|
|
156
|
-
<Foo superLongParam="bar"
|
|
157
|
-
anotherSuperLongParam="baz" />
|
|
158
|
-
|
|
159
|
-
// good
|
|
160
|
-
<Foo
|
|
161
|
-
superLongParam="bar"
|
|
162
|
-
anotherSuperLongParam="baz"
|
|
163
|
-
/>
|
|
164
|
-
|
|
165
|
-
// if props fit in one line then keep it on the same line
|
|
166
|
-
<Foo bar="bar" />
|
|
167
|
-
|
|
168
|
-
// children get indented normally
|
|
169
|
-
<Foo
|
|
170
|
-
superLongParam="bar"
|
|
171
|
-
anotherSuperLongParam="baz"
|
|
172
|
-
>
|
|
173
|
-
<Quux />
|
|
174
|
-
</Foo>
|
|
175
|
-
|
|
176
|
-
// bad
|
|
177
|
-
{showButton &&
|
|
178
|
-
<Button />
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// bad
|
|
182
|
-
{
|
|
183
|
-
showButton &&
|
|
184
|
-
<Button />
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// good
|
|
188
|
-
{showButton && (
|
|
189
|
-
<Button />
|
|
190
|
-
)}
|
|
191
|
-
|
|
192
|
-
// good
|
|
193
|
-
{showButton && <Button />}
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
## Quotes
|
|
197
|
-
|
|
198
|
-
- Always use double quotes (`"`) for JSX attributes, but single quotes (`'`) for all other JS. eslint: [`jsx-quotes`](https://eslint.org/docs/rules/jsx-quotes)
|
|
199
|
-
|
|
200
|
-
> Why? Regular HTML attributes also typically use double quotes instead of single, so JSX attributes mirror this convention.
|
|
201
|
-
|
|
202
|
-
```jsx
|
|
203
|
-
// bad
|
|
204
|
-
<Foo bar='bar' />
|
|
205
|
-
|
|
206
|
-
// good
|
|
207
|
-
<Foo bar="bar" />
|
|
208
|
-
|
|
209
|
-
// bad
|
|
210
|
-
<Foo style={{ left: "20px" }} />
|
|
211
|
-
|
|
212
|
-
// good
|
|
213
|
-
<Foo style={{ left: '20px' }} />
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
## Spacing
|
|
217
|
-
|
|
218
|
-
- 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)
|
|
219
|
-
|
|
220
|
-
```jsx
|
|
221
|
-
// bad
|
|
222
|
-
<Foo/>
|
|
223
|
-
|
|
224
|
-
// very bad
|
|
225
|
-
<Foo />
|
|
226
|
-
|
|
227
|
-
// bad
|
|
228
|
-
<Foo
|
|
229
|
-
/>
|
|
230
|
-
|
|
231
|
-
// good
|
|
232
|
-
<Foo />
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
- 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)
|
|
236
|
-
|
|
237
|
-
```jsx
|
|
238
|
-
// bad
|
|
239
|
-
<Foo bar={ baz } />
|
|
240
|
-
|
|
241
|
-
// good
|
|
242
|
-
<Foo bar={baz} />
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
## Props
|
|
246
|
-
|
|
247
|
-
- Always use camelCase for prop names.
|
|
248
|
-
|
|
249
|
-
```jsx
|
|
250
|
-
// bad
|
|
251
|
-
<Foo
|
|
252
|
-
UserName="hello"
|
|
253
|
-
phone_number={12345678}
|
|
254
|
-
/>
|
|
255
|
-
|
|
256
|
-
// good
|
|
257
|
-
<Foo
|
|
258
|
-
userName="hello"
|
|
259
|
-
phoneNumber={12345678}
|
|
260
|
-
/>
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
- 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)
|
|
264
|
-
|
|
265
|
-
```jsx
|
|
266
|
-
// bad
|
|
267
|
-
<Foo
|
|
268
|
-
hidden={true}
|
|
269
|
-
/>
|
|
270
|
-
|
|
271
|
-
// good
|
|
272
|
-
<Foo
|
|
273
|
-
hidden
|
|
274
|
-
/>
|
|
275
|
-
|
|
276
|
-
// good
|
|
277
|
-
<Foo hidden />
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
- 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)
|
|
281
|
-
|
|
282
|
-
```jsx
|
|
283
|
-
// bad
|
|
284
|
-
<img src="hello.jpg" />
|
|
285
|
-
|
|
286
|
-
// good
|
|
287
|
-
<img src="hello.jpg" alt="Me waving hello" />
|
|
288
|
-
|
|
289
|
-
// good
|
|
290
|
-
<img src="hello.jpg" alt="" />
|
|
291
|
-
|
|
292
|
-
// good
|
|
293
|
-
<img src="hello.jpg" role="presentation" />
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
- 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)
|
|
297
|
-
|
|
298
|
-
> Why? Screenreaders already announce `img` elements as images, so there is no need to include this information in the alt text.
|
|
299
|
-
|
|
300
|
-
```jsx
|
|
301
|
-
// bad
|
|
302
|
-
<img src="hello.jpg" alt="Picture of me waving hello" />
|
|
303
|
-
|
|
304
|
-
// good
|
|
305
|
-
<img src="hello.jpg" alt="Me waving hello" />
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
- 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)
|
|
309
|
-
|
|
310
|
-
```jsx
|
|
311
|
-
// bad - not an ARIA role
|
|
312
|
-
<div role="datepicker" />
|
|
313
|
-
|
|
314
|
-
// bad - abstract ARIA role
|
|
315
|
-
<div role="range" />
|
|
316
|
-
|
|
317
|
-
// good
|
|
318
|
-
<div role="button" />
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
- 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)
|
|
322
|
-
|
|
323
|
-
> Why? Inconsistencies between keyboard shortcuts and keyboard commands used by people using screenreaders and keyboards complicate accessibility.
|
|
324
|
-
|
|
325
|
-
```jsx
|
|
326
|
-
// bad
|
|
327
|
-
<div accessKey="h" />
|
|
328
|
-
|
|
329
|
-
// good
|
|
330
|
-
<div />
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
- 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)
|
|
334
|
-
|
|
335
|
-
> 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.
|
|
336
|
-
|
|
337
|
-
We don’t recommend using indexes for keys if the order of items may change.
|
|
338
|
-
|
|
339
|
-
```jsx
|
|
340
|
-
// bad
|
|
341
|
-
{todos.map((todo, index) =>
|
|
342
|
-
<Todo
|
|
343
|
-
{...todo}
|
|
344
|
-
key={index}
|
|
345
|
-
/>
|
|
346
|
-
)}
|
|
347
|
-
|
|
348
|
-
// good
|
|
349
|
-
{todos.map(todo => (
|
|
350
|
-
<Todo
|
|
351
|
-
{...todo}
|
|
352
|
-
key={todo.id}
|
|
353
|
-
/>
|
|
354
|
-
))}
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
- Use spread props sparingly.
|
|
358
|
-
> 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).
|
|
359
|
-
|
|
360
|
-
Exceptions:
|
|
361
|
-
|
|
362
|
-
- HOCs that proxy down props and hoist propTypes
|
|
363
|
-
|
|
364
|
-
```jsx
|
|
365
|
-
function HOC(WrappedComponent) {
|
|
366
|
-
return class Proxy extends React.Component {
|
|
367
|
-
Proxy.propTypes = {
|
|
368
|
-
text: PropTypes.string,
|
|
369
|
-
isLoading: PropTypes.bool
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
render() {
|
|
373
|
-
return <WrappedComponent {...this.props} />
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
- Spreading objects with known, explicit props. This can be particularly useful when testing React components with Mocha’s beforeEach construct.
|
|
380
|
-
|
|
381
|
-
```jsx
|
|
382
|
-
export default function Foo {
|
|
383
|
-
const props = {
|
|
384
|
-
text: '',
|
|
385
|
-
isPublished: false
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return (<div {...props} />);
|
|
389
|
-
}
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
Notes for use:
|
|
393
|
-
Filter out unnecessary props when possible. Also, use [prop-types-exact](https://www.npmjs.com/package/prop-types-exact) to help prevent bugs.
|
|
394
|
-
|
|
395
|
-
```jsx
|
|
396
|
-
// bad
|
|
397
|
-
render() {
|
|
398
|
-
const { irrelevantProp, ...relevantProps } = this.props;
|
|
399
|
-
return <WrappedComponent {...this.props} />
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// good
|
|
403
|
-
render() {
|
|
404
|
-
const { irrelevantProp, ...relevantProps } = this.props;
|
|
405
|
-
return <WrappedComponent {...relevantProps} />
|
|
406
|
-
}
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
## Refs
|
|
410
|
-
|
|
411
|
-
- Always use ref callbacks, createRef or useRef hook. eslint: [`react/no-string-refs`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md)
|
|
412
|
-
|
|
413
|
-
```jsx
|
|
414
|
-
// bad
|
|
415
|
-
<Foo
|
|
416
|
-
ref="myRef"
|
|
417
|
-
/>
|
|
418
|
-
|
|
419
|
-
// good
|
|
420
|
-
<Foo
|
|
421
|
-
ref={(ref) => { this.myRef = ref; }}
|
|
422
|
-
/>
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
## Parentheses
|
|
426
|
-
|
|
427
|
-
- 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)
|
|
428
|
-
|
|
429
|
-
```jsx
|
|
430
|
-
// bad
|
|
431
|
-
render() {
|
|
432
|
-
return <MyComponent variant="long body" foo="bar">
|
|
433
|
-
<MyChild />
|
|
434
|
-
</MyComponent>;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
// good
|
|
438
|
-
render() {
|
|
439
|
-
return (
|
|
440
|
-
<MyComponent variant="long body" foo="bar">
|
|
441
|
-
<MyChild />
|
|
442
|
-
</MyComponent>
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// good, when single line
|
|
447
|
-
render() {
|
|
448
|
-
const body = <div>hello</div>;
|
|
449
|
-
return <MyComponent>{body}</MyComponent>;
|
|
450
|
-
}
|
|
451
|
-
```
|
|
452
|
-
|
|
453
|
-
## Tags
|
|
454
|
-
|
|
455
|
-
- 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)
|
|
456
|
-
|
|
457
|
-
```jsx
|
|
458
|
-
// bad
|
|
459
|
-
<Foo variant="stuff"></Foo>
|
|
460
|
-
|
|
461
|
-
// good
|
|
462
|
-
<Foo variant="stuff" />
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
- 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)
|
|
466
|
-
|
|
467
|
-
```jsx
|
|
468
|
-
// bad
|
|
469
|
-
<Foo
|
|
470
|
-
bar="bar"
|
|
471
|
-
baz="baz" />
|
|
472
|
-
|
|
473
|
-
// good
|
|
474
|
-
<Foo
|
|
475
|
-
bar="bar"
|
|
476
|
-
baz="baz"
|
|
477
|
-
/>
|
|
478
|
-
```
|
|
479
|
-
|
|
480
|
-
## Methods
|
|
481
|
-
- 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)
|
|
482
|
-
|
|
483
|
-
> 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.
|
|
484
|
-
|
|
485
|
-
```jsx
|
|
486
|
-
// bad
|
|
487
|
-
class extends React.Component {
|
|
488
|
-
handleClickDiv() {
|
|
489
|
-
// do stuff
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
render() {
|
|
493
|
-
return <div onClick={this.handleClickDiv.bind(this)} />;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
// bad
|
|
498
|
-
class extends React.Component {
|
|
499
|
-
handleClickDiv = () => {
|
|
500
|
-
// do stuff
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
render() {
|
|
504
|
-
return <div onClick={this.handleClickDiv} />
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
// good
|
|
509
|
-
class extends React.Component {
|
|
510
|
-
constructor(props) {
|
|
511
|
-
super(props);
|
|
512
|
-
|
|
513
|
-
this.handleClickDiv = this.handleClickDiv.bind(this);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
handleClickDiv() {
|
|
517
|
-
// do stuff
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
render() {
|
|
521
|
-
return <div onClick={this.handleClickDiv} />;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
- Do not use underscore prefix for internal methods of a React component.
|
|
527
|
-
> 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.
|
|
528
|
-
|
|
529
|
-
```jsx
|
|
530
|
-
// bad
|
|
531
|
-
React.createClass({
|
|
532
|
-
_handleClickSubmit() {
|
|
533
|
-
// do stuff
|
|
534
|
-
},
|
|
535
|
-
|
|
536
|
-
// other stuff
|
|
537
|
-
});
|
|
538
|
-
|
|
539
|
-
// good
|
|
540
|
-
class extends React.Component {
|
|
541
|
-
handleClickSubmit() {
|
|
542
|
-
// do stuff
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
// other stuff
|
|
546
|
-
}
|
|
547
|
-
```
|
|
548
|
-
|
|
549
|
-
- Prefix event handlers with `handle`
|
|
550
|
-
> Why? It helps differentiate between props and class methods.
|
|
551
|
-
|
|
552
|
-
```jsx
|
|
553
|
-
// bad
|
|
554
|
-
class extends React.Component {
|
|
555
|
-
onClickSubmit() {
|
|
556
|
-
// do stuff
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// other stuff
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
// good
|
|
563
|
-
class extends React.Component {
|
|
564
|
-
handleClickSubmit() {
|
|
565
|
-
// do stuff
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
// other stuff
|
|
569
|
-
}
|
|
570
|
-
```
|
|
571
|
-
|
|
572
|
-
- 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)
|
|
573
|
-
|
|
574
|
-
```jsx
|
|
575
|
-
// bad
|
|
576
|
-
render() {
|
|
577
|
-
(<div />);
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
// good
|
|
581
|
-
render() {
|
|
582
|
-
return (<div />);
|
|
583
|
-
}
|
|
584
|
-
```
|
|
585
|
-
|
|
586
|
-
## Ordering
|
|
587
|
-
|
|
588
|
-
- Ordering for `class extends React.Component`:
|
|
589
|
-
|
|
590
|
-
1. `constructor`
|
|
591
|
-
1. `componentWillMount`
|
|
592
|
-
1. `componentDidMount`
|
|
593
|
-
1. `componentWillReceiveProps`
|
|
594
|
-
1. `shouldComponentUpdate`
|
|
595
|
-
1. `componentWillUpdate`
|
|
596
|
-
1. `componentDidUpdate`
|
|
597
|
-
1. `componentWillUnmount`
|
|
598
|
-
1. `getChildContext`
|
|
599
|
-
1. `getDefaultState`
|
|
600
|
-
1. *getter and setter methods`* like `getSelectReason()` or `setValue()`
|
|
601
|
-
1. *clickHandlers or eventHandlers* like `handleClickSubmit()` or `handleChangeDescription()`
|
|
602
|
-
1. *optional render methods* like `renderNavigation()` or `renderProfilePicture()`
|
|
603
|
-
1. `render`
|
|
604
|
-
|
|
605
|
-
- How to define `propTypes`, `defaultProps`, `contextTypes`, etc...
|
|
606
|
-
|
|
607
|
-
```jsx
|
|
608
|
-
import React from 'react';
|
|
609
|
-
import PropTypes from 'prop-types';
|
|
610
|
-
|
|
611
|
-
const propTypes = {
|
|
612
|
-
id: PropTypes.number.isRequired,
|
|
613
|
-
url: PropTypes.string.isRequired,
|
|
614
|
-
text: PropTypes.string,
|
|
615
|
-
};
|
|
616
|
-
|
|
617
|
-
const defaultProps = {
|
|
618
|
-
text: 'Hello World',
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
class Link extends React.Component {
|
|
622
|
-
static methodsAreOk() {
|
|
623
|
-
return true;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
render() {
|
|
627
|
-
return <a href={this.props.url} data-id={this.props.id}>{this.props.text}</a>;
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
Link.propTypes = propTypes;
|
|
632
|
-
Link.defaultProps = defaultProps;
|
|
633
|
-
|
|
634
|
-
export default Link;
|
|
635
|
-
```
|
|
636
|
-
|
|
637
|
-
## Hooks
|
|
638
|
-
Try to use functional components with Hooks whenever possible. Follow [rules of hooks](https://reactjs.org/docs/hooks-rules.html) and be aware of the need to use useMemo and useCallback to avoid unnecessary rerenders.
|
|
639
|
-
|
|
640
|
-
Pay close attention to useEffect and try to define all of the variables inside of the hook body to avoid creating
|
|
641
|
-
too many dependencies.
|
|
642
|
-
|
|
643
|
-
```jsx
|
|
644
|
-
const Button = () => {
|
|
645
|
-
const myFn = () => {...};
|
|
646
|
-
// bad, myFn is now a dynamic dependency
|
|
647
|
-
useEffect(() => myFn(), []);
|
|
648
|
-
}
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
```jsx
|
|
652
|
-
const Button = () => {
|
|
653
|
-
// good
|
|
654
|
-
useEffect(() => {
|
|
655
|
-
const myFn = () => {...};
|
|
656
|
-
return myFn();
|
|
657
|
-
}, []);
|
|
658
|
-
}
|
|
659
|
-
```
|
|
660
|
-
|
|
661
|
-
## CSS Modules
|
|
662
|
-
|
|
663
|
-
Contrary to 'global css', CSS modules do not allow overriding styles inside components out of the box.
|
|
664
|
-
|
|
665
|
-
- Use `className` prop to override styles of the root element.
|
|
666
|
-
|
|
667
|
-
```jsx
|
|
668
|
-
// signup_button.js
|
|
669
|
-
import styles from "signup_button.module.scss"
|
|
670
|
-
|
|
671
|
-
<Button className={styles.button} />
|
|
672
|
-
```
|
|
673
|
-
|
|
674
|
-
```jsx
|
|
675
|
-
// button.js
|
|
676
|
-
import styles from "button.module.scss"
|
|
677
|
-
|
|
678
|
-
const Button = ({ className: passedClassName }) => {
|
|
679
|
-
const className = clsx(styles.button, passedClassName);
|
|
680
|
-
return <div className={className}>Click me</button>;
|
|
681
|
-
};
|
|
682
|
-
```
|
|
683
|
-
|
|
684
|
-
- Use `theme` prop to override multiple styles inside a component
|
|
685
|
-
|
|
686
|
-
```jsx
|
|
687
|
-
// signup_button.js
|
|
688
|
-
import buttonStyles from "signup_button.module.scss"
|
|
689
|
-
|
|
690
|
-
<Button theme={buttonStyles} />
|
|
691
|
-
```
|
|
692
|
-
|
|
693
|
-
```jsx
|
|
694
|
-
// button.js
|
|
695
|
-
import styles from "button.module.scss"
|
|
696
|
-
|
|
697
|
-
const Button = ({ theme }) => {
|
|
698
|
-
const className = clsx(styles.button, theme.button);
|
|
699
|
-
const iconClassName = clsx(styles.icon, theme.icon);
|
|
700
|
-
return (
|
|
701
|
-
<div className={className}>
|
|
702
|
-
Click me
|
|
703
|
-
<span className={iconClassName}>icon</span>
|
|
704
|
-
</button>
|
|
705
|
-
);
|
|
706
|
-
};
|
|
707
|
-
```
|
|
708
|
-
|
|
709
|
-
- If you need to pass custom style names make sure they are mapped outside of the Component render call. That way the object remains the same between renders.
|
|
710
|
-
|
|
711
|
-
```jsx
|
|
712
|
-
const Button = () => {
|
|
713
|
-
// bad
|
|
714
|
-
const theme = { ... };
|
|
715
|
-
return <Button theme={buttonStyles} />;
|
|
716
|
-
}
|
|
717
|
-
```
|
|
718
|
-
|
|
719
|
-
```jsx
|
|
720
|
-
// good
|
|
721
|
-
const theme = { ... };
|
|
722
|
-
|
|
723
|
-
const Button = () => {
|
|
724
|
-
return <Button theme={buttonStyles} />;
|
|
725
|
-
}
|
|
726
|
-
```
|