react18-legacy-context
它能做什么
这个工具可以帮你把 React 代码中旧的上下文(context)写法更新为较新的 `createContext` 方式。它会一步步指导你替换那些负责在组件之间共享数据的过时部分。
适用场景
- ✓当你的 React 代码中出现关于 `contextTypes` 或 `getChildContext` 的警告时。
- ✓当你要把 React 应用升级到较新版本(React 18 或更高版本)时。
- ✓在修改任何与旧版 React 组件数据共享相关的代码之前。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/react18-legacy-context/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何把 React 旧的上下文写法更新为新的 `createContext`?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会先找出数据在哪里被共享(提供方,provider),以及在哪里被使用(消费方,consumer)。然后它会给出一份分步计划,指导你创建一个新的上下文文件,并把所有提供方和消费方都更新为新的 API。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
React 18 Legacy Context Migration
Legacy context (contextTypes, childContextTypes, getChildContext) was deprecated in React 16.3 and warns in React 18.3.1. It is removed in React 19.
This Is Always a Cross-File Migration
Unlike most other migrations that touch one file at a time, context migration requires coordinating:
- Create the context object (usually a new file)
- Update the provider component
- Update every consumer component
Missing any consumer leaves the app broken - it will read from the wrong context or get undefined.
Migration Steps (Always Follow This Order)
Step 1: Find the provider (childContextTypes + getChildContext)
Step 2: Find ALL consumers (contextTypes)
Step 3: Create the context file
Step 4: Update the provider
Step 5: Update each consumer (class components → contextType, function components → useContext)
Step 6: Verify - run the app, check no legacy context warnings remain
Scan Commands
# Find all providers
grep -rn "childContextTypes\|getChildContext" src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
# Find all consumers
grep -rn "contextTypes\s*=" src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
# Find this.context usage (may be legacy or modern - check which)
grep -rn "this\.context\." src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
Reference Files
references/single-context.md- complete migration for one context (theme, auth, etc.) with provider + class consumer + function consumerreferences/multi-context.md- apps with multiple legacy contexts (nested providers, multiple consumers of different contexts)references/context-file-template.md- the standard file structure for a new context module
常见问题
`contextTypes`、`childContextTypes` 和 `getChildContext` 是什么?
它们是 React 中旧的组件间数据共享方式。现在已经被更现代、更灵活的 `createContext` 方式取代了。
为什么要更新所有的消费方?
如果你没有更新每一处使用旧数据的地方,你的应用可能无法正常工作,或者显示错误的信息。
什么是“提供方”(provider)和“消费方”(consumer)?
“提供方”是提供数据的组件,“消费方”是接收并使用这些数据的组件。
详情
作者的原始描述
Provides the complete migration pattern for React legacy context API (contextTypes, childContextTypes, getChildContext) to the modern createContext API. Use this skill whenever migrating legacy context in class components - this is always a cross-file migration requiring the provider AND all consumers to be updated together. Use it before touching any contextTypes or childContextTypes code, becaus
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7386
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。