mcpField
R
react18-batching-patterns

react18-batching-patterns

github.com/githubDevelopment40.5k754 installsUpdated today
Repository

About react18-batching-patterns

A reference skill for automatic batching in React 18 class-component codebases, focused on diagnosing silent issues from asynchronous state updates. It distinguishes state-read bugs, refactoring cases, and situations that may require flushSync, while advising restraint with synchronous flushes.

Key features

  1. 01React 17 and 18 batching behavior comparison
  2. 02Diagnosis of state reads after await
  3. 03Distinction between refactoring and flushSync cases
  4. 04Guidance on when flushSync applies

Use cases

  1. 01Debugging async state issues in class components
  2. 02Checking state reads after await
  3. 03Deciding whether a loading state must appear immediately

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/github/awesome-copilot --skill react18-batching-patterns

After installing, type/react18-batching-patternsin Claude Code to confirm it loaded.

Bundled filesreferences/batching-categories.mdreferences/flushSync-guide.md

Details

From the project README

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 setStateReact 17React 18
React event handlerBatchedBatched (same)
setTimeoutImmediate re-renderBatched
Promise .then() / .catch()Immediate re-renderBatched
async/awaitImmediate re-renderBatched
Native addEventListener callbackImmediate re-renderBatched

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.

Comments

Markdown supported