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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何迁移 React 组件中用来设置初始状态的 `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 项目评估的政策。它有助于规范项目的评估方式,并确保合规。