react19-source-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

React 19 Source Migration Patterns

React 19 源码迁移模式

Reference for every source-file migration required for React 19.
React 19 所需的所有源码文件迁移参考。

Quick Reference Table

快速参考表

PatternActionReference
ReactDOM.render(...)
createRoot().render()
See references/api-migrations.md
ReactDOM.hydrate(...)
hydrateRoot(...)
See references/api-migrations.md
unmountComponentAtNode
root.unmount()
Inline fix
ReactDOM.findDOMNode
→ direct refInline fix
forwardRef(...)
wrapper
→ ref as direct propSee references/api-migrations.md
Component.defaultProps = {}
→ ES6 default paramsSee references/api-migrations.md
useRef()
no arg
useRef(null)
Inline fix add
null
Legacy Context
createContext
→ api-migrations.md#legacy-context
String refs
this.refs.x
createRef()
→ api-migrations.md#string-refs
import React from 'react'
(unused)
RemoveOnly if no
React.
usage in file
模式操作参考
ReactDOM.render(...)
createRoot().render()
见 references/api-migrations.md
ReactDOM.hydrate(...)
hydrateRoot(...)
见 references/api-migrations.md
unmountComponentAtNode
root.unmount()
内联修复
ReactDOM.findDOMNode
→ 直接使用ref内联修复
forwardRef(...)
包装器
→ ref作为直接prop传递见 references/api-migrations.md
Component.defaultProps = {}
→ ES6 默认参数见 references/api-migrations.md
无参数的
useRef()
useRef(null)
内联修复,添加
null
参数
旧版Context
createContext
→ api-migrations.md#legacy-context
字符串ref
this.refs.x
createRef()
→ api-migrations.md#string-refs
未使用的
import React from 'react'
移除仅当文件中没有使用
React.
相关内容时适用

PropTypes Rule

PropTypes 规则

Do not remove
.propTypes
assignments. The
prop-types
package still works as a standalone validator. React 19 only removes the built-in runtime checking from the React package the package itself remains valid.
Add this comment above any
.propTypes
block:
jsx
// NOTE: React 19 no longer runs propTypes validation at runtime.
// PropTypes kept for documentation and IDE tooling only.
不要移除
.propTypes
赋值。
prop-types
包仍然可以作为独立的校验器使用。React 19 仅移除了React包内置的运行时校验能力,这个包本身仍然可用。
在每个
.propTypes
代码块上方添加如下注释:
jsx
// NOTE: React 19 no longer runs propTypes validation at runtime.
// PropTypes kept for documentation and IDE tooling only.

Read the Reference

查阅参考文档

For full before/after code for each migration, read
references/api-migrations.md
. It contains the complete patterns including edge cases for
forwardRef
with
useImperativeHandle
,
defaultProps
null vs undefined behavior, and legacy context provider/consumer cross-file migrations.
如需获取每个迁移对应的完整代码前后对比,请阅读
references/api-migrations.md
。该文档包含完整的迁移模式,包括
forwardRef
搭配
useImperativeHandle
的边缘情况、
defaultProps
的null与undefined行为差异,以及旧版context provider/consumer跨文件迁移等内容。