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

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

可直接粘贴的消息

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

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

  1. Create the context object (usually a new file)
  2. Update the provider component
  3. 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 consumer
  • references/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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能