All skills
github avatar

/react18-batching-patterns

@90921cc official
by githubgithub/awesome-copilot40k stars
5,040

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.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/react18-batching-patterns

This session only. Nothing lands on disk.

SKILL.md

≈135 tokens always: the name and description. ≈480 when used: this file. ≈2.1k more on demand in 2 files.

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.

Source: SKILL.md on GitHub

No alerts15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    This skill provides technical documentation and code patterns for diagnosing and fixing React 18 automatic batching regressions. It is purely informational and contains no executable code or external dependencies.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 90921cc. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 19 hours ago.

Activeupdated 5 months ago

README badge

README badge for github/awesome-copilot/react18-batching-patterns

Provides decision trees and patterns for diagnosing and fixing React 18's automatic batching behavior in class components, particularly setState calls in async methods, Promises, and native event handlers that silently broke in React 18. Use this before adding flushSync calls to determine whether you need synchronous re-renders or should refactor to avoid reading stale state after await.

Generated from the current SKILL.md.

Does this skill apply to functional components with hooks?
No. This skill targets class components only. React 18 batching behavior for functional components with useState is different and not covered here.
When should I use flushSync to fix a batching regression?
Only when the user must see an intermediate UI state before an async operation begins, like a loading spinner before a fetch. Most batching regressions should be fixed by refactoring to avoid reading this.state after await, not by adding flushSync.
Does this skill help with test failures after upgrading to React 18?
Yes. It covers fixing test failures caused by intermediate state assertions that break after React 18 upgrade, since intermediate re-renders no longer occur in setTimeout, Promise .then(), or async/await contexts.
What async contexts are affected by React 18 batching changes?
setTimeout, Promise .then()/.catch(), async/await, and native addEventListener callbacks now batch setState calls together in React 18, whereas React 17 would re-render immediately after each one.

Generated from the current SKILL.md. These answers refresh after source changes.