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
Disallow **direct** calls to the [`set` function](https://react.dev/reference/react/useState#setstate) of `useState` in `useEffect`.
30
30
31
-
This rule only checks for **direct** calls to the `set` function of `useState` in `useEffect`. It does not check for calls to `set` function in callbacks, event handlers, or `Promise.then` functions.
31
+
## Why this rule exists
32
+
33
+
Directly setting state in `useEffect` can lead to:
34
+
35
+
-**Redundant state**: You might be duplicating derived values that could be computed during render.
36
+
-**Unnecessary effects**: Triggering re-renders that could be avoided.
37
+
-**Confusing logic**: It can make component behavior harder to reason about.
38
+
39
+
## What counts as a violation?
40
+
41
+
This is **not allowed**:
42
+
43
+
```tsx
44
+
useEffect(() => {
45
+
setFullName(firstName+""+lastName);
46
+
}, [firstName, lastName]);
47
+
```
48
+
49
+
Instead, compute the value during render:
50
+
51
+
```tsx
52
+
const fullName =firstName+""+lastName;
53
+
```
54
+
55
+
## What is allowed?
56
+
57
+
The rule **does not flag** indirect calls, such as:
58
+
59
+
- Inside event handlers.
60
+
- Inside `async` functions.
61
+
- Inside `setTimeout`, `setInterval`, `Promise.then`, etc.
62
+
63
+
## Known limitations
64
+
65
+
- It doesn’t check `set` calls in `useEffect` cleanup functions.
32
66
33
67
## Examples
34
68
@@ -246,13 +280,6 @@ function List({ items }) {
246
280
}
247
281
```
248
282
249
-
## Known limitations
250
-
251
-
- The `set` call to `useState` in the `cleanup` function of `useEffect` will not be checked.
252
-
- The current implementation does not support determining whether a `set` function called in an `async` function is actually at least one `await` after.
Disallow **direct** calls to the [`set` function](https://react.dev/reference/react/useState#setstate) of `useState` in `useLayoutEffect`.
24
24
25
-
This rule only checks for **direct** calls to the `set` function of `useState` in `useEffect`. It does not check for calls to `set` function in callbacks, event handlers, or `Promise.then` functions.
25
+
## Why this rule exists
26
+
27
+
Directly setting state in `useLayoutEffect` can lead to:
28
+
29
+
-**Redundant state**: You might be duplicating derived values that could be computed during render.
30
+
-**Unnecessary effects**: Triggering re-renders that could be avoided.
31
+
-**Confusing logic**: It can make component behavior harder to reason about.
32
+
33
+
## What counts as a violation?
34
+
35
+
This is **not allowed**:
36
+
37
+
```tsx
38
+
useLayoutEffect(() => {
39
+
setFullName(firstName+""+lastName);
40
+
}, [firstName, lastName]);
41
+
```
42
+
43
+
Instead, compute the value during render:
44
+
45
+
```tsx
46
+
const fullName =firstName+""+lastName;
47
+
```
48
+
49
+
## What is allowed?
50
+
51
+
The rule **does not flag** indirect calls, such as:
52
+
53
+
- Inside event handlers.
54
+
- Inside `async` functions.
55
+
- Inside `setTimeout`, `setInterval`, `Promise.then`, etc.
56
+
57
+
## Known limitations
58
+
59
+
- It doesn’t check `set` calls in `useLayoutEffect` cleanup functions.
26
60
27
61
## Examples
28
62
@@ -240,13 +274,6 @@ function List({ items }) {
240
274
}
241
275
```
242
276
243
-
## Known limitations
244
-
245
-
- The `set` call to `useState` in the `cleanup` function of `useLayoutEffect` will not be checked.
246
-
- The current implementation does not support determining whether a `set` function called in an `async` function is actually at least one `await` after.
0 commit comments