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: README.md
+33-2Lines changed: 33 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,21 @@
1
1
# ESLint - React - You Might Not Need An Effect
2
2
3
-
ESLint plugin to catch [unnecessary React `useEffect`s](https://react.dev/learn/you-might-not-need-an-effect) to make your code easier to follow, faster to run, and less error-prone. Highly recommended for new React developers as you learn its mental model, and even experienced developers may be surprised.
3
+
ESLint plugin to catch [unnecessary React `useEffect`s](https://react.dev/learn/you-might-not-need-an-effect) to make your code easier to follow, faster to run, and less error-prone.
4
+
5
+
Highly recommended for new React developers as you learn its mental model, and even experienced developers may be surprised!
6
+
7
+
## 🆚 Comparison
8
+
9
+
The new [`eslint-plugin-react-hooks/set-state-in-effect`](https://react.dev/reference/eslint-plugin-react-hooks/lints/set-state-in-effect) flags synchronous `setState` calls inside effects, helping prevent unnecessary re-renders. Often, this means you don’t need an effect at all!
10
+
11
+
However, unnecessary effects aren’t limited to synchronous `setState` calls. In contrast, `eslint-plugin-react-you-might-not-need-an-effect`:
12
+
13
+
1. Reports specific anti-patterns, providing actionable suggestions and links.
14
+
2. Covers more than the documented [You Might Not Need An Effect](https://react.dev/learn/you-might-not-need-an-effect) cases.
15
+
3. Analyzes props and refs — the other half of using React internals in effects.
16
+
4. Considers effects' dependencies, since when the effect runs influences its impact.
17
+
5. Incorporates advanced heuristics to minimize false negatives and false positives.
18
+
6. Obsesses over unusual logic and syntax — because you never know what might end up in an effect.
4
19
5
20
## 📦 Installation
6
21
@@ -86,6 +101,20 @@ function Form() {
86
101
}
87
102
```
88
103
104
+
Disallow storing state derived from *any* state (even external) when the setter is only called once:
* Walks up the AST until a `useEffect` call, returning `false` if never found, or finds any of the following on the way:
238
-
* - An async function
239
-
* - A function declaration, which may be called at an arbitrary later time
238
+
* - An `async` function
239
+
* - A function declaration, which may be called at an arbitrary later time.
240
+
* - While we return false for *this* call, we may still return true for a call to a function containing this call. Combined with `getUpstreamRefs()`, it will still flag calls to the containing function.
240
241
* - A function passed as a callback to another function or `new` - event handler, `setTimeout`, `Promise.then()` `new ResizeObserver()`, etc.
0 commit comments