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: packages/eslint-plugin-react-hooks/README.md
+89-2Lines changed: 89 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,93 @@ We suggest to use this option **very sparingly, if at all**. Generally saying, w
71
71
72
72
Please refer to the [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html) documentation and the [Hooks FAQ](https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce) to learn more about this rule.
73
73
74
-
## License
74
+
## no-nested-components
75
75
76
-
MIT
76
+
Based of [`react/no-unstable-nested-components`](https://github.com/jsx-eslint/eslint-plugin-react/blob/8beb2aae3fbe36dd6f495b72cb20b27c043aff68/docs/rules/no-unstable-nested-components.md) but with a [detection mechanism consistent with Rules of Hooks](https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce).
77
+
78
+
Creating components inside components (nested components) will cause React to throw away the state of those nested components on each re-render of their parent.
79
+
80
+
React reconciliation performs element type comparison with [reference equality](https://reactjs.org/docs/reconciliation.html#elements-of-different-types). The reference to the same element changes on each re-render when defining components inside the render block. This leads to complete recreation of the current node and all its children. As a result the virtual DOM has to do extra unnecessary work and [possible bugs are introduced](https://codepen.io/ariperkkio/pen/vYLodLB).
81
+
82
+
### `no-nested-components` details
83
+
84
+
The following patterns are considered warnings:
85
+
86
+
```jsx
87
+
functionComponent() {
88
+
// nested component declaration
89
+
functionUnstableNestedComponent() {
90
+
return<div />;
91
+
}
92
+
93
+
return (
94
+
<div>
95
+
<UnstableNestedComponent />
96
+
</div>
97
+
);
98
+
}
99
+
```
100
+
101
+
102
+
```jsx
103
+
functionuseComponent() {
104
+
// Nested component declaration in a hook. See https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce for what's considered a Component and hook.
105
+
returnfunctionComponent() {
106
+
return<div />
107
+
}
108
+
}
109
+
```
110
+
111
+
```jsx
112
+
functionComponent() {
113
+
constconfig=React.useMemo({
114
+
// Nested component declaration. See https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce for what's considered a component.
115
+
ArrowDown(event) {
116
+
117
+
}
118
+
})
119
+
120
+
return (
121
+
<div>
122
+
<UnstableNestedComponent />
123
+
</div>
124
+
);
125
+
}
126
+
```
127
+
128
+
129
+
The following patterns are **not** considered warnings:
130
+
131
+
```jsx
132
+
functionOutsideDefinedComponent(props) {
133
+
return<div />;
134
+
}
135
+
136
+
functionComponent() {
137
+
return (
138
+
<div>
139
+
<OutsideDefinedComponent />
140
+
</div>
141
+
);
142
+
}
143
+
```
144
+
145
+
⚠️ WARNING ⚠️:
146
+
147
+
Creating nested but memoized components should also be avoided since memoization is a performance concern not a semantic guarantee.
148
+
If the `useCallback` or `useMemo` hook has no dependency, you can safely move the component definition out of the render function.
149
+
If the hook does have dependencies, you should refactor the code so that you're able to move the component definition out of the render function.
150
+
If you want React to throw away the state of the nested component, use a [`key`](https://reactjs.org/docs/lists-and-keys.html#keys) instead.
151
+
152
+
```jsx
153
+
functionComponent() {
154
+
// No ESLint warning but `MemoizedNestedComponent` should be moved outside of `Component`.
0 commit comments