react18-string-refs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact 18 String Refs Migration
React 18 字符串Ref迁移
String refs ( + ) were deprecated in React 16.3, warn in React 18.3.1, and are removed in React 19.
ref="myInput"this.refs.myInput字符串ref( + )在React 16.3版本中已被弃用,在React 18.3.1中会触发警告,且在React 19中已被完全移除。
ref="myInput"this.refs.myInputQuick Pattern Map
快速模式映射
| Pattern | Reference |
|---|---|
| Single ref on a DOM element | → patterns.md#single-ref |
| Multiple refs in one component | → patterns.md#multiple-refs |
| Refs in a list / dynamic refs | → patterns.md#list-refs |
| Callback refs (alternative approach) | → patterns.md#callback-refs |
| Ref passed to a child component | → patterns.md#forwarded-refs |
| 模式 | 参考资料 |
|---|---|
| DOM元素上的单个ref | → patterns.md#single-ref |
| 单个组件内的多个ref | → patterns.md#multiple-refs |
| 列表中的ref / 动态ref | → patterns.md#list-refs |
| 回调ref(替代方案) | → patterns.md#callback-refs |
| 传递给子组件的Ref | → patterns.md#forwarded-refs |
Scan Command
扫描命令
bash
undefinedbash
undefinedFind all string ref assignments in JSX
Find all string ref assignments in JSX
grep -rn 'ref="' src/ --include=".js" --include=".jsx" | grep -v ".test."
grep -rn 'ref="' src/ --include=".js" --include=".jsx" | grep -v ".test."
Find all this.refs accessors
Find all this.refs accessors
grep -rn "this.refs." src/ --include=".js" --include=".jsx" | grep -v ".test."
Both should be migrated together - find the `ref="name"` and the `this.refs.name` accesses for each component as a pair.grep -rn "this.refs." src/ --include=".js" --include=".jsx" | grep -v ".test."
上述两项需要一同迁移:找到每个组件对应的`ref="name"`和`this.refs.name`访问语句,成对处理。The Migration Rule
迁移规则
Every string ref migrates to :
React.createRef()- Add as a class field (or in constructor)
refName = React.createRef(); - Replace →
ref="refName"in JSXref={this.refName} - Replace →
this.refs.refNameeverywherethis.refName.current
Read for the full before/after for each case.
references/patterns.md所有字符串ref都需迁移为:
React.createRef()- 添加作为类字段(或写在构造函数中)
refName = React.createRef(); - 将JSX中的替换为
ref="refName"ref={this.refName} - 将所有位置的替换为
this.refs.refNamethis.refName.current
阅读查看每种场景的完整前后对比示例。
references/patterns.md