React 18 Batching Patterns

React 18 Batching Patterns

Diagnoses and fixes automatic batching regressions when upgrading React class components to React 18, with a decision tree for when flushSync is really needed.

Skill files3 files

references/

SKILL.md48 lines
1
---
2
name: react18-batching-patterns
3
description: 'Provides exact patterns for diagnosing and fixing automatic batching regressions in React 18 class components. Use this skill whenever a class component has multiple setState calls in an async method, inside setTimeout, inside a Promise .then() or .catch(), or in a native event handler. Use it before writing any flushSync call - the decision tree here prevents unnecessary flushSync overuse. Also use this skill when fixing test failures caused by intermediate state assertions that break after React 18 upgrade.'
4
---
5
6
# React 18 Automatic Batching Patterns
7
8
Reference for diagnosing and fixing the most dangerous silent breaking change in React 18 for class-component codebases.
9
10
## The Core Change
11
12
| Location of setState | React 17 | React 18 |
13
|---|---|---|
14
| React event handler | Batched | Batched (same) |
15
| setTimeout | **Immediate re-render** | **Batched** |
16
| Promise .then() / .catch() | **Immediate re-render** | **Batched** |
17
| async/await | **Immediate re-render** | **Batched** |
18
| Native addEventListener callback | **Immediate re-render** | **Batched** |
19
20
**Batched** means: all setState calls within that execution context flush together in a single re-render at the end. No intermediate renders occur.
21
22
## Quick Diagnosis
23
24
Read every async class method. Ask: does any code after an `await` read `this.state` to make a decision?
25
26
```
27
Code reads this.state after await?
28
YES → Category A (silent state-read bug)
29
NO, but intermediate render must be visible to user?
30
YES → Category C (flushSync needed)
31
NO → Category B (refactor, no flushSync)
32
```
33
34
For the full pattern for each category, read:
35
- **`references/batching-categories.md`** - Category A, B, C with full before/after code
36
- **`references/flushSync-guide.md`** - when to use flushSync, when NOT to, import syntax
37
38
## The flushSync Rule
39
40
**Use `flushSync` sparingly.** It forces a synchronous re-render, bypassing React 18's concurrent scheduler. Overusing it negates the performance benefits of React 18.
41
42
Only use `flushSync` when:
43
- The user must see an intermediate UI state before an async operation begins
44
- A spinner/loading state must render before a fetch starts
45
- Sequential UI steps have distinct visible states (progress wizard, multi-step flow)
46
47
In most cases, the fix is a **refactor** - restructuring the code to not read `this.state` after `await`. Read `references/batching-categories.md` for the correct approach per category.
48

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,964 prompts

More by @OpenSourceBuffGuy

See all
Giant Soda Can Frozen in Glacier prompt image

Giant Soda Can Frozen in Glacier

Draws a monumental soda can frozen in blue glacial ice in an arctic landscape, with a snowcat and tiny scientists for scale, as a cinematic low-angle advertising photo.

image-prompt, photorealistic, cinematic, low-angle, wide-shot, product-photography, advertising, winter, landscape, surreal, forced-perspective, vertical-format, soft-lighting, natural-light, muted-colors, prompt-weights, nano-banana

Werewolf hero clashes with jungle behemoth prompt image

Werewolf hero clashes with jungle behemoth

Directs a 30-second photorealistic creature-action film in which a hero turns into a werewolf and fights a moss-armored jungle behemoth, with character locks and timed beats.

video-prompt, action, creatures, fantasy-world, character-consistency, timed-beats, multi-shot, transformation, visual-effects, slow-motion, cinematic, photorealistic, forest, rain, dramatic-lighting, seedance, camera-movement, tracking-shot, long-form, structured-output, guardrails, martial-arts, film-score, reference-to-video

Ancient Civilization Miniature Diorama prompt image

Ancient Civilization Miniature Diorama

Draws a realistic miniature diorama of an ancient civilization's monuments under construction, set on blueprints atop a wooden drafting table.

image-prompt, gpt-image, miniature, photorealistic, architecture, history, blueprint, 3d-render, cinematic, natural-light, vertical-format, placeholders, long-form, construction, still-life, bokeh, architectural-rendering, for-artists, concept-art, fantasy-world

Reviews

Loading reviews…

Discussion

Loading comments…