You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/require-memo.md
+17-10Lines changed: 17 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,23 @@
1
1
# Rule: require-memo
2
2
3
-
This rule enforces the use of `React.memo()` on function components. The objective is to optimize your component re-renders and avoid unnecessary render cycles when your component's props do not change.
3
+
This rule enforces the use of `memo()` on function components. The objective is to optimize your component re-renders and avoid unnecessary render cycles when your component's props do not change.
4
4
5
5
## Rationale
6
6
7
-
React’s rendering behavior ensures that whenever the parent component renders, the child component instances are re-rendered as well. When dealing with expensive computations or components, this could lead to performance issues. `React.memo()` is a higher order component which tells React to skip rendering the component if its props have not changed.
7
+
React’s rendering behavior ensures that whenever the parent component renders, the child component instances are re-rendered as well. When dealing with expensive computations or components, this could lead to performance issues. `memo()` is a higher order component which tells React to skip rendering the component if its props have not changed.
8
8
9
-
When `React.memo()` wraps an exported component, then it will only re-render if the current and next props are not shallowly equal.
9
+
When `memo()` wraps an exported component, then it will only re-render if the current and next props are not shallowly equal.
10
10
11
11
```jsx
12
12
functionMyComponent(props) { /* ... */ }
13
13
14
-
exportdefaultReact.memo(MyComponent);
14
+
exportdefaultmemo(MyComponent);
15
15
```
16
16
17
17
This rule applies to function components, not class-based components as they should extend `React.PureComponent` or must implement `shouldComponentUpdate` lifecycle method for similar optimization.
18
18
19
19
## Rule Details
20
-
This rule will enforce that all function components are wrapped in `React.memo()`.
20
+
This rule will enforce that all function components are wrapped in `memo()`.
21
21
Only exported components are validated.
22
22
23
23
## Incorrect Code Examples
@@ -43,23 +43,30 @@ function ComponentB(props) {
43
43
return<div>{props.name}</div>;
44
44
}
45
45
46
-
exportdefaultReact.memo(ComponentB);
46
+
exportdefaultmemo(ComponentB);
47
47
```
48
48
## Options
49
49
50
50
The rule takes an optional object:
51
51
52
52
```json
53
53
"rules": {
54
-
"@myorg/react-memo/require-memo": [2, {
55
-
"ignoreComponents": ["IgnoreMe"]
54
+
"@arthurgeron/react-usememo/require-memo": [2, {
55
+
"ignoredComponents": {
56
+
"IgnoreMe": true,
57
+
"DontIgnoreMe": false,
58
+
"!IgnoreEverythingButMe": true,
59
+
}
56
60
}]
57
61
}
58
62
```
59
-
-`ignoreComponents`: An array of component names to ignore when checking for `React.memo()` usage.
63
+
-`{ignoredComponents: Record<string, boolean>}`: This allows you to add specific Component Names, thereby individually disabling or enabling them to be checked when used. Matching names with a `true` value will cause the checks to be ignored.
64
+
You can use strict 1:1 comparisons (e.g., `"ComponentName"`) or employ Minimatch's Glob Pattern (e.g., `"*Item"`).
65
+
> For more information on Minimatch, refer to its README [here](https://www.npmjs.com/package/minimatch). You may also find this [cheatsheet](https://github.com/motemen/minimatch-cheat-sheet) useful.
66
+
60
67
61
68
## When Not To Use It
62
69
63
-
If the component always re-renders with different props or is not expensive in terms of performance, there is no real benefit to using `React.memo()`. In fact, using `React.memo()` for a large number of simple components could negatively impact performance as memoizing small components may cost more than re-rendering them. So this rule should be disabled in such scenarios.
70
+
If the component always re-renders with different props or is not expensive in terms of performance, there is no real benefit to using `memo()`. In fact, using `memo()` for a large number of simple components could negatively impact performance as memoizing small components may cost more than re-rendering them.
64
71
65
72
> For more examples and detailed explanation, refer to the eslint-plugin-react-memo [readme](https://github.com/myorg/eslint-plugin-react-memo).
0 commit comments