本文へスキップ
Skillpedia
WebサイトとUI(フロントエンド)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に貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/github/awesome-copilot/blob/main/skills/react18-batching-patterns/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に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.

よくある質問

「バッチング」とは何ですか?

React が複数の画面更新を一つにまとめて処理することです。ユーザーインターフェースの再描画が何度も起きないようにするため、パフォーマンスが向上します。

React 18 では何が変わりましたか?

React 18 では、setTimeout や Promise などの非同期関数の中でも、バッチングが自動で働くようになりました。これは良いことですが、即座の更新を前提とした古いコードでは動作が変わることがあります。

`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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル