Skip to content

Commit fb64f06

Browse files
committed
docs: improve rule docs for setState in effect hooks
1 parent 2a3f1cb commit fb64f06

File tree

2 files changed

+70
-16
lines changed

2 files changed

+70
-16
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-effect.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,41 @@ react-hooks-extra/no-direct-set-state-in-use-effect
2828

2929
Disallow **direct** calls to the [`set` function](https://react.dev/reference/react/useState#setstate) of `useState` in `useEffect`.
3030

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.
3266

3367
## Examples
3468

@@ -246,13 +280,6 @@ function List({ items }) {
246280
}
247281
```
248282

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.
253-
254-
The limitation may be lifted in the future.
255-
256283
## Implementation
257284

258285
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-effect.ts)

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-layout-effect.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,41 @@ react-hooks-extra/no-direct-set-state-in-use-layout-effect
2222

2323
Disallow **direct** calls to the [`set` function](https://react.dev/reference/react/useState#setstate) of `useState` in `useLayoutEffect`.
2424

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.
2660

2761
## Examples
2862

@@ -240,13 +274,6 @@ function List({ items }) {
240274
}
241275
```
242276

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.
247-
248-
The limitation may be lifted in the future.
249-
250277
## Implementation
251278

252279
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-direct-set-state-in-use-layout-effect.ts)

0 commit comments

Comments
 (0)