react18-legacy-context
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact 18 Legacy Context Migration
React 18 旧版Context迁移
Legacy context (, , ) was deprecated in React 16.3 and warns in React 18.3.1. It is removed in React 19.
contextTypeschildContextTypesgetChildContext旧版context(、、)在React 16.3版本中被弃用,在React 18.3.1版本中会触发警告,在React 19版本中已被完全移除。
contextTypeschildContextTypesgetChildContextThis 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和大多数每次仅修改单个文件的迁移不同,context迁移需要协调以下操作:
- 创建context对象(通常单独新建文件)
- 更新Provider组件
- 更新所有消费者组件
遗漏任何一个消费者都会导致应用崩溃——它会读取错误的context或者得到。
undefinedMigration 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 remainStep 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 remainScan Commands
扫描命令
bash
undefinedbash
undefinedFind all providers
Find all providers
grep -rn "childContextTypes|getChildContext" src/ --include=".js" --include=".jsx" | grep -v ".test."
grep -rn "childContextTypes|getChildContext" src/ --include=".js" --include=".jsx" | grep -v ".test."
Find all consumers
Find all consumers
grep -rn "contextTypes\s*=" src/ --include=".js" --include=".jsx" | grep -v ".test."
grep -rn "contextTypes\s*=" src/ --include=".js" --include=".jsx" | grep -v ".test."
Find this.context usage (may be legacy or modern - check which)
Find this.context usage (may be legacy or modern - check which)
grep -rn "this.context." src/ --include=".js" --include=".jsx" | grep -v ".test."
undefinedgrep -rn "this.context." src/ --include=".js" --include=".jsx" | grep -v ".test."
undefinedReference Files
参考文件
- - complete migration for one context (theme, auth, etc.) with provider + class consumer + function consumer
references/single-context.md - - apps with multiple legacy contexts (nested providers, multiple consumers of different contexts)
references/multi-context.md - - the standard file structure for a new context module
references/context-file-template.md
- - 单个context(主题、鉴权等)的完整迁移示例,包含Provider + 类组件消费者 + 函数组件消费者
references/single-context.md - - 适用于存在多个旧版context的应用(嵌套Provider、多个不同context的消费者)
references/multi-context.md - - 新建context模块的标准文件结构
references/context-file-template.md