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
{{ message }}
Repository navigation
Commit d083ec1
Browse filesBrowse the repository at this point in the historyBrowse files
[compiler] Allow setState after await in effect functions (react#36734)
`react-hooks/set-state-in-effect` flagged setState calls that happen
after an `await` inside an async function invoked from an effect.
Post-await code resumes in a microtask after the effect body has
returned, so the "synchronous setState cascades a render" rationale does
not apply; the lint was a false positive on a common data-loading shape
(15 reactions on the issue).
The fix exempts a setState only when it is *provably* post-await: a
forward must-dataflow over the HIR CFG computes the blocks that begin
after an await has executed on every path from the function entry
(optimistic initialization so loop back-edges do not pessimize the meet,
fixpoint to the greatest solution), plus an intra-block flag for
instructions after an Await in the same block. A setState reachable on
any await-free path still flags, so the conditional-await case remains
an error by design, with a fixture documenting that choice. Suppression
is sound under try/catch because HIRBuilder terminates blocks after each
instruction in a try region, and an awaited rejection also resumes in a
microtask.
Fixtures: post-await setState (event gone), setState before the first
await (still flags), setState after a conditional await (still flags).
First commit documents the false positive via the lint-mode logger
output, second removes it.
Builds on the approach in react#36417 by @raashish1601, hardened from a
seen-await flag to the path-sensitive analysis above. Implemented
identically in the TypeScript compiler and the Rust port.
Verification: TS snap 1807/1807, Rust snap 1807/1807, cargo workspace
green, scoped TS-vs-Rust HIR parity harness green.
Closesreact#34905
---------
Co-authored-by: Raashish Aggarwal <94279692+raashish1601@users.noreply.github.com>
// The path where `delay` is false reaches this setState without passing an
44
+
// await, so it can still execute synchronously within the effect: flag it.
45
+
setReady(true);
46
+
};
47
+
useEffect(() => {
48
+
load();
49
+
}, [load]);
50
+
return ready ?"Ready":"Loading";
51
+
}
52
+
53
+
exportconstFIXTURE_ENTRYPOINT= {
54
+
fn: Component,
55
+
params: [{ delay:false }],
56
+
};
57
+
58
+
```
59
+
60
+
## Logs
61
+
62
+
```
63
+
{"kind":"CompileError","detail":{"category":"EffectSetState","reason":"Calling setState synchronously within an effect can trigger cascading renders","description":"Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:\n* Update external systems with the latest state from React.\n* Subscribe for updates from some external system, calling setState in a callback function when external state changes.\n\nCalling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect)","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":15,"column":4,"index":478},"end":{"line":15,"column":8,"index":482},"filename":"invalid-setState-in-useEffect-after-conditional-await.ts","identifierName":"load"},"message":"Avoid calling setState() directly within an effect"}]},"fnLoc":null}
{"kind":"CompileError","detail":{"category":"EffectSetState","reason":"Calling setState synchronously within an effect can trigger cascading renders","description":"Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:\n* Update external systems with the latest state from React.\n* Subscribe for updates from some external system, calling setState in a callback function when external state changes.\n\nCalling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect)","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":11,"column":4,"index":286},"end":{"line":11,"column":8,"index":290},"filename":"invalid-setState-in-useEffect-before-await.ts","identifierName":"load"},"message":"Avoid calling setState() directly within an effect"}]},"fnLoc":null}
0 commit comments