react18-batching-patterns
它能做什么
这个工具可以帮你修复在 React 18 中使用旧式“类组件”时常见的一个问题。它能让界面更新一起完成,而不是逐个分开执行,从而提升性能。
适用场景
- ✓当你有多次界面更新,但它们没有同时显示出来时。
- ✓如果你的代码在异步函数(async/await、Promise、setTimeout)中多次更新状态时。
- ✓当你升级到 React 18 后,出现性能问题或界面显示错误时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/react18-batching-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“怎样才能让我的类组件在 React 18 下正确地更新界面?”
每周 5 个新技能,1 分钟读完。
工作原理
它会分析你代码中调用界面更新(setState)的位置。如果这些调用位于 React 18 会自动合并更新的地方(比如事件处理中),它会给出修改建议。如果不在,它会帮你判断,是强制立即更新(flushSync),还是重构代码。
技术内容(原始 SKILL.md)
作者在 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 codereferences/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.
常见问题
什么是“批处理”(batching)?
“批处理”指的是 React 把多次界面更新合并成一次,这样用户界面就不必多次重绘,从而提升性能。
React 18 有哪些变化?
在 React 18 中,“批处理”(合并更新)在更多地方自动生效,比如在异步函数(setTimeout、Promise)里。这是好事,但可能会影响旧代码,因为旧代码可能依赖即时更新。
什么时候应该使用 flushSync?
只有在用户必须在长时间操作开始前立即看到界面更新时,才使用 flushSync。大多数情况下,更好的做法是修改代码(重构),而不是使用 flushSync。
详情
作者的原始描述
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
热度
- GitHub 星标
- 40k
- 排名
- #7383
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。