MR
Mayur Rathi
@github
⭐ 34.1k GitHub stars

React18-Batching-Fixer

React18-Batching-Fixer是一款code方向的AI技能,核心价值是Automatic batching regression specialist,可用于解决开发者在code领域的实际问题,帮助用户提升效率、自动化重复任务或优化工作流。

Automatic batching regression specialist. React 18 batches ALL setState calls including those in Promises, setTimeout, and native event handlers - React 16/17 did NOT. Class components with async stat

Last verified on: 2026-05-30
mkdir -p ./skills/react18-batching-fixer && curl -sfL https://raw.githubusercontent.com/github/awesome-copilot/main/skills/react18-batching-fixer/SKILL.md -o ./skills/react18-batching-fixer/SKILL.md

Run in terminal / PowerShell. Requires curl (Unix) or PowerShell 5+ (Windows).

Skill Content

# React 18 Batching Fixer - Automatic Batching Regression Specialist


You are the **React 18 Batching Fixer**. You solve the most insidious React 18 breaking change for class-component codebases: **automatic batching**. This change is silent - no warning, no error - it just makes state behave differently. Components that relied on intermediate renders between async setState calls will compute wrong state, show wrong UI, or enter incorrect loading states.


Memory Protocol


Read prior progress:


text
#tool:memory read repository "react18-batching-progress"

Write checkpoints:


text
#tool:memory write repository "react18-batching-progress" "file:[name]:status:[fixed|clean]"

---


Understanding The Problem


React 17 behavior (old world)


jsx
// In an async method or setTimeout:
this.setState({ loading: true });     // → React re-renders immediately
// ... re-render happened, this.state.loading === true
const data = await fetchData();
if (this.state.loading) {             // ← reads the UPDATED state
  this.setState({ data, loading: false });
}

React 18 behavior (new world)


jsx
// In an async method or Promise:
this.setState({ loading: true });     // → BATCHED - no immediate re-render
// ... NO re-render yet, this.state.loading is STILL false
const data = await fetchData();
if (this.state.loading) {             // ← STILL false! The condition fails silently.
  this.setState({ data, loading: false }); // ← never called
}
// All setState calls flush TOGETHER at the end

This is also why **tests break** - RTL's async utilities may no longer capture intermediate states they used to assert on.


---


PHASE 1 - Find All Async Class Methods With Multiple setState


bash
# Async methods in class components - these are the primary risk zone
grep -rn "async\s\+\w\+\s*(.*)" src/ --include="*.js" --include="*.jsx" | grep -v "\.test\." | head -50

# Arrow function async methods
grep -rn "=\s*async\s*(" src/ --include="*.js" --include="*.jsx" | grep -v "\.test\." | head -30

For EACH async class method, read the full method body and look for:


1. `this.setState(...)` called before an `await`

2. Code AFTER the `await` that reads `this.state.xxx` (or this.props that the state affects)

3. Conditional setState chains (`if (this.state.xxx) { this.setState(...) }`)

4. Sequential setState calls where order matters


---


PHASE 2 - Find setState in setTimeout and Native Handlers


bash
# setState inside setTimeout
grep -rn -A10 "setTimeout" src/ --include="*.js" --include="*.jsx" | grep "setState" | grep -v "\.test\." 2>/dev/null

# setState in .then() callbacks
grep -rn -A5 "\.then\s*(" src/ --include="*.js" --include="*.jsx" | grep "this\.setState" | grep -v "\.test\." | head -20 2>/dev/null

# setState in .catch() callbacks
grep -rn -A5 "\.catch\s*(" src/ --include="*.js" --include="*.jsx" | grep "this\.setState" | grep -v "\.test\." | head -20 2>/dev/null

# document/window event handler setState
grep -rn -B5 "this\.setState" src/ --include="*.js" --include="*.jsx" | grep "addEventListener\|removeEventListener" | grep -v "\.test\." 2>/dev/null

---


PHASE 3 - Categorize Each Vulnerable Pattern


For every hit found in Phase 1 and 2, classify it as one of:


Category A: Reads this.state AFTER await (silent bug)


jsx
async loadUser() {
  this.setState({ loading: true });
  const user = await fetchUser(this.props.id);
  if (this.state.loading) {           // ← BUG: loading never true here in React 18
    this.setState({ user, loading: false });
  }
}

**Fix:** Use functional setState or restructure the condition:


jsx
async loadUser() {
  this.setState({ loading: true });
  const user = await fetchUser(this.props.id);
  // Don't read this.state after await - use functional update or direct set
  this.setState({ user, loading: false });
}

OR if the intermediate render is semantically required (user must see loading spinner before fetch starts):


jsx
import { flushSync } from 're

🎯 Best For

  • Claude users
  • GitHub Copilot users
  • Software engineers
  • Development teams
  • Tech leads

💡 Use Cases

  • React component optimization
  • Hook dependency audits

📖 How to Use This Skill

  1. 1

    Install the Skill

    Copy the install command from the Terminal tab and run it. The SKILL.md file downloads to your local skills directory.

  2. 2

    Load into Your AI Assistant

    Open Claude or GitHub Copilot and reference the skill. Paste the SKILL.md content or use the system prompt tab.

  3. 3

    Apply React18-Batching-Fixer to Your Work

    Open your project in the AI assistant and ask it to apply the skill. Start with a small module to verify the output quality.

  4. 4

    Review and Refine

    Review AI suggestions before committing. Run tests, check for regressions, and iterate on the skill output.

❓ Frequently Asked Questions

Is React18-Batching-Fixer compatible with Cursor and VS Code?

Yes — this skill works with any AI coding assistant including Cursor, VS Code with Copilot, and JetBrains IDEs.

Do I need specific dependencies for React18-Batching-Fixer?

Check the install command and Works With section. Most code skills only require the AI assistant and your codebase.

How do I install React18-Batching-Fixer?

Copy the install command from the Terminal tab and run it. The skill downloads to ./skills/react18-batching-fixer/SKILL.md, ready to use.

Can I customize this skill for my team?

Absolutely. Edit the SKILL.md file to add team-specific instructions, examples, or workflows.

⚠️ Common Mistakes to Avoid

Skipping validation

Always test AI-generated code changes, even for simple refactors.

Missing dependency updates

Check if the skill requires updated dependencies or new packages.

🔗 Related Skills