跳到正文
Skillpedia
网站与界面(前端)40k · #7383

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/react18-batching-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 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.

常见问题

什么是“批处理”(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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能