Catalog
github/react18-batching-patterns

github

react18-batching-patterns

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.

v1.0Latest
New~607Updated Jun 26, 2026

React 18 Automatic Batching Patterns

Reference for diagnosing and fixing the most dangerous silent breaking change in React 18 for class-component codebases.

The Core Change

Location of setState React 17 React 18
React event handler Batched Batched (same)
setTimeout Immediate re-render Batched
Promise .then() / .catch() Immediate re-render Batched
async/await Immediate re-render Batched
Native addEventListener callback Immediate re-render Batched

Batched means: all setState calls within that execution context flush together in a single re-render at the end. No intermediate renders occur.

Quick Diagnosis

Read every async class method. Ask: does any code after an await read this.state to make a decision?

Code reads this.state after await?
  YES → Category A (silent state-read bug)
  NO, but intermediate render must be visible to user?
    YES → Category C (flushSync needed)
    NO → Category B (refactor, no flushSync)

For the full pattern for each category, read:

  • references/batching-categories.md - Category A, B, C with full before/after code
  • references/flushSync-guide.md - when to use flushSync, when NOT to, import syntax

The flushSync Rule

Use flushSync sparingly. It forces a synchronous re-render, bypassing React 18's concurrent scheduler. Overusing it negates the performance benefits of React 18.

Only use flushSync when:

  • The user must see an intermediate UI state before an async operation begins
  • A spinner/loading state must render before a fetch starts
  • Sequential UI steps have distinct visible states (progress wizard, multi-step flow)

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.

Files3
3 files · 9.3 KB

Select a file to preview

Overall Score

88/100

Grade

A

Excellent

Safety

92

Quality

87

Clarity

89

Completeness

82

Summary

This skill provides diagnostic patterns and code examples for fixing React 18 automatic batching regressions in class components. It teaches developers how to identify three distinct batching problems (state reads after await, independent setState consolidation, and intermediate render visibility), diagnose which category applies, and apply the correct fix—refactoring or selective flushSync use—without over-applying synchronous flushes that harm performance.

Detected Capabilities

code analysispattern matchingdiagnostic guidancerefactoring suggestions

Trigger Keywords

Phrases that MCP clients use to match this skill to user intent.

react 18 batchingsetState not workingflushSync when neededasync state readsreact test failures

Use Cases

  • Fix state-read bugs after async/await in class components
  • Resolve test assertions that break after React 18 upgrade
  • Diagnose why multiple setState calls produce unexpected state values
  • Determine when flushSync is necessary versus when refactoring is the better fix
  • Update setTimeout or Promise .then() handlers that relied on intermediate re-renders

Quality Notes

  • Excellent decision-tree structure in main skill that guides diagnosis before applying fixes
  • Three well-defined and distinct categories (A/B/C) with clear diagnostic questions and before/after code examples
  • Includes explicit anti-patterns and warnings (e.g., 'don't use flushSync to fix Category A bugs')
  • Supporting files are well-organized with separate guides for category patterns and flushSync usage
  • Import syntax and API guidance is precise (flushSync comes from react-dom, not react)
  • Test pattern guidance acknowledges breaking changes and provides async/waitFor alternatives
  • Performance guidance emphasizes sparingly using flushSync to avoid negating React 18 benefits
  • Code examples are realistic and cover common scenarios (loading spinners, multi-step flows, setTimeout)
  • Diagnostic rules are actionable—developers can read code and apply the decision tree immediately
Model: claude-haiku-4-5-20251001Analyzed: Jun 26, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Use github/react18-batching-patterns in your dev environment

Command Palette

Search for a command to run...