Skip to content
Skillpedia
Websites & UI (frontend)40k · #5326

react18-batching-patterns

Provides patterns to fix 'batching' issues in React 18 components. It helps resolve multiple 'setState' calls in async methods.

What it does

This tool helps fix a common issue in older code ('class components') using React 18. It ensures screen updates happen together instead of separately, improving performance.

When to use it

  • ✓When you have multiple screen updates that don't appear all at once.
  • ✓If your code updates the state multiple times within asynchronous functions (async/await, Promises, setTimeout).
  • ✓If you're experiencing performance issues or visual bugs after updating to React 18.

Example of what to ask your AI

“How can I make my class component's screen updates work correctly with React 18?”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/github/awesome-copilot/blob/main/skills/react18-batching-patterns/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/github/awesome-copilot/blob/main/skills/react18-batching-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

It analyzes where you call screen updates (setState) in your code. If they are in places React 18 automatically batches (like inside events), it suggests how to fix it. If not, it helps decide whether to force an immediate update (flushSync) or refactor the code.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add github/awesome-copilot --skill react18-batching-patterns

Or download just the file:

curl -L https://raw.githubusercontent.com/github/awesome-copilot/main/skills/react18-batching-patterns/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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.

FAQ

What is 'batching'?

'Batching' is when React groups multiple screen updates into one so the user interface doesn't have to redraw multiple times, which improves performance.

What changed in React 18?

In React 18, 'batching' (grouping updates) started working automatically in more places, like inside asynchronous functions (setTimeout, Promises). This is good, but can break older code that relied on immediate updates.

When should I use `flushSync`?

Use `flushSync` only when it's essential for the user to see an immediate screen update before a long operation begins. In most cases, it's better to fix the code ('refactor') instead of using `flushSync`.

Details

Original description by the author

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 u

repo: github/awesome-copilot

Popularity

GitHub stars
40k
Ranking position
#5326
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills