react18-lifecycle-patterns
できること
Reactコードの古い「ライフサイクル」メソッドを、新しいバージョン向けに更新するためのツールです。コードの各部分を適切な場所に移し、最近のReactバージョンでも正しく動くようにする方法を示します。
使う場面
- ✓現在は推奨されなくなったライフサイクルメソッドを使っている古いReactコードがあるとき。
- ✓「getDerivedStateFromProps」と「componentDidUpdate」のどちらを使うか判断するとき。
- ✓更新を処理するために「getSnapshotBeforeUpdate」を追加する必要があるとき。
スキルのリンク
https://github.com/github/awesome-copilot/blob/main/skills/react18-lifecycle-patterns/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“Reactコンポーネントで初期stateを設定している「componentWillMount」メソッドを移行するにはどうすればよいですか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
このガイドでは、古いメソッドの働きを、React 18で同じことを行う新しい方法と比較します。古いメソッドの役割に応じて、具体的な例を参照できるように案内します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
React 18 Lifecycle Patterns
Reference for migrating the three unsafe class component lifecycle methods to React 18.3.1 compliant patterns.
Quick Decision Guide
Before migrating any lifecycle method, identify the semantic category of what the method does. Wrong category = wrong migration. The table below routes you to the correct reference file.
componentWillMount - what does it do?
| What it does | Correct migration | Reference |
|---|---|---|
Sets initial state (this.setState(...)) |
Move to constructor |
→ componentWillMount.md |
| Runs a side effect (fetch, subscription, DOM) | Move to componentDidMount |
→ componentWillMount.md |
| Derives initial state from props | Move to constructor with props |
→ componentWillMount.md |
componentWillReceiveProps - what does it do?
| What it does | Correct migration | Reference |
|---|---|---|
| Async side effect triggered by prop change (fetch, cancel) | componentDidUpdate |
→ componentWillReceiveProps.md |
| Pure state derivation from new props (no side effects) | getDerivedStateFromProps |
→ componentWillReceiveProps.md |
componentWillUpdate - what does it do?
| What it does | Correct migration | Reference |
|---|---|---|
| Reads the DOM before update (scroll, size, position) | getSnapshotBeforeUpdate |
→ componentWillUpdate.md |
| Cancels requests / runs effects before update | componentDidUpdate with prev comparison |
→ componentWillUpdate.md |
The UNSAFE_ Prefix Rule
Never use UNSAFE_componentWillMount, UNSAFE_componentWillReceiveProps, or UNSAFE_componentWillUpdate as a permanent fix.
Prefixing suppresses the React 18.3.1 warning but does NOT:
- Fix concurrent mode safety issues
- Prepare the codebase for React 19 (where these are removed, with or without the prefix)
- Fix the underlying semantic problem the migration is meant to address
The UNSAFE_ prefix is only appropriate as a temporary hold while scheduling the real migration sprint. Mark any UNSAFE_ prefix additions with:
// TODO: React 19 will remove this. Migrate before React 19 upgrade.
// UNSAFE_ prefix added temporarily - replace with componentDidMount / getDerivedStateFromProps / etc.
Reference Files
Read the full reference file for the lifecycle method you are migrating:
references/componentWillMount.md- 3 cases with full before/after codereferences/componentWillReceiveProps.md- getDerivedStateFromProps trap warnings, full examplesreferences/componentWillUpdate.md- getSnapshotBeforeUpdate + componentDidUpdate pairing
Read the relevant file before writing any migration code.
よくある質問
「ライフサイクルメソッド」とは何ですか?
コンポーネントが作成されたとき、更新されたとき、削除されたときなど、特定のタイミングで実行される、Reactコンポーネント内の特別な関数です。
「UNSAFE_componentWillMount」のような古いメソッドをそのまま使ってはいけないのですか?
「UNSAFE_」という接頭辞を付けると警告が隠れるだけで、新しいReactバージョンとの互換性の問題は解決しません。一時的な回避策であり、根本的な解決にはなりません。
移行用のコード例はどこにありますか?
詳しい例は「componentWillMount.md」「componentWillReceiveProps.md」「componentWillUpdate.md」などの個別のリファレンスファイルにあり、このガイドからそれらを参照できます。
詳細
作者による元の説明
Provides exact before/after migration patterns for the three unsafe class component lifecycle methods - componentWillMount, componentWillReceiveProps, and componentWillUpdate - targeting React 18.3.1. Use this skill whenever a class component needs its lifecycle methods migrated, when deciding between getDerivedStateFromProps vs componentDidUpdate, when adding getSnapshotBeforeUpdate, or when fixi
repo: github/awesome-copilot
人気
- GitHubのスター
- 40k
- ランキング
- #7387
- ライセンス
- MIT
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
acquire-codebase-knowledge
40kエージェントが、既存のコードプロジェクトを理解してドキュメント化できるよう支援します。新しく参加するメンバーのオンボーディングや、アーキテクチャの全体像の把握に役立ちます。
acreadiness-assess
40kエージェントが、プロジェクトのAI対応状況を評価してレポートを生成できるよう支援します。プロジェクトがAIツールをどの程度活用できる状態にあるかを把握するのに役立ちます。
acreadiness-generate-instructions
40kエージェントが、評価結果をもとにAIツール向けの指示文を作成できるよう支援します。AIがコードとどのようにやり取りするかを改善し、特定された不足点を解消するのに役立ちます。
acreadiness-policy
40kエージェントが、AIプロジェクトの評価ポリシーの策定や適用を支援できるようにします。プロジェクトの評価方法を標準化し、コンプライアンスを確保するのに役立ちます。
似ているスキル
angular-developer
280kエージェントがAngularのコードを作成し、プロジェクト構成についてアドバイスできるようにします。新しいプロジェクトの開始、コード部品の作成、パフォーマンスやアクセシビリティの改善に役立ちます。
frontend-a11y
280kエージェントが Web インターフェースを誰にとっても使いやすくするよう支援します。スクリーンリーダーやキーボード操作に対応し、サイトやアプリが適切に動作するようにします。
frontend-patterns
280kエージェントがフロントエンド開発の最新パターンを適用できるようにします。React と Next.js において、パフォーマンスの最適化、状態管理、ベストプラクティスの遵守を行います。