Skip to content

[compiler] Allow setState after await in effect functions#36734

Open
poteto wants to merge 2 commits into
react:mainfrom
poteto:lauren/fix-34905-setstate-await
Open

[compiler] Allow setState after await in effect functions#36734
poteto wants to merge 2 commits into
react:mainfrom
poteto:lauren/fix-34905-setstate-await

Conversation

@poteto

@poteto poteto commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

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

Closes #34905

poteto and others added 2 commits June 9, 2026 22:51
ValidateNoSetStateInEffects flags setState calls made after an await in an
async function invoked from a useEffect, even though post-await code runs in
a microtask rather than synchronously in the effect body, so the cascading
render rationale does not apply. Documents the current wrong behavior for
react#34905: the lint-mode logger output shows the EffectSetState
error pointing at load() despite the setState only ever running after the
await.
ValidateNoSetStateInEffects treated every setState call inside a function
transitively invoked from a useEffect body as a synchronous cascading
render, including calls that can only execute after an await. Code after
an await resumes in a microtask after the effect body has returned, so
the cascading-render rationale does not apply. Fixes react#34905.

Both compilers now compute a forward must-dataflow over the HIR CFG and
suppress a setState iff every path from the function entry to the call
passes through an Await. A naive block-local seenAwait flag would be
unsound: an await in one branch of a conditional must not suppress a
setState that is also reachable through the other, await-free branch, so
the analysis intersects over all predecessors. The new
invalid-setState-in-useEffect-after-conditional-await fixture documents
that a conditional await still flags, and
invalid-setState-in-useEffect-before-await covers setState ahead of the
first await. Exception edges are safe to treat like normal edges because
HIRBuilder.push gives every instruction inside a try region its own
maybe-throw block, and an Await's rejection also resumes in a microtask.

Adopts the post-await fixpoint approach from react#36417, reworked to track
per-block state in both the TypeScript and Rust implementations.

Co-authored-by: Raashish Aggarwal <94279692+raashish1601@users.noreply.github.com>
@meta-cla meta-cla Bot added the CLA Signed label Jun 10, 2026
@poteto poteto marked this pull request as ready for review June 10, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compiler Bug]: react-hooks/set-state-in-effect false positive on async function

1 participant