react18-string-refs
它能做什么
这个工具可以帮你把旧版 React 代码更新到较新的写法。它会把文本形式的引用(比如 `ref='myInput'`)转换为更现代的格式(`React.createRef()`),确保你的代码能正常运行。
适用场景
- ✓当你正在把一个旧的 React 项目升级到较新版本时。
- ✓如果你的 React 代码中出现关于“字符串 refs”(string refs)的警告时。
- ✓在你开始手动改写代码中的引用之前。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/react18-string-refs/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何把 React 中的“字符串 refs”更新为现代写法?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会先找出你在 React 代码中使用文本引用的地方,然后展示具体的替换方式,把它们改为 `React.createRef()`。这是当前 React 版本中的正确做法。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
React 18 String Refs Migration
String refs (ref="myInput" + this.refs.myInput) were deprecated in React 16.3, warn in React 18.3.1, and are removed in React 19.
Quick 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 |
Scan Command
# Find all string ref assignments in JSX
grep -rn 'ref="' src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
# 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.
The Migration Rule
Every string ref migrates to React.createRef():
- Add
refName = React.createRef();as a class field (or in constructor) - Replace
ref="refName"→ref={this.refName}in JSX - Replace
this.refs.refName→this.refName.currenteverywhere
Read references/patterns.md for the full before/after for each case.
常见问题
什么是“字符串 refs”(string refs)?
这是 React 中一种旧的引用元素方式,通过文本名称来引用,比如 `ref='myInput'`。这种方式已经被更安全、更现代的方式取代了。
为什么需要更新?
“字符串 refs”已经被弃用,并将在未来的 React 版本中移除。及早更新可以确保你的代码继续正常运行,并兼容新版本。
什么是 `React.createRef()`?
它是在 React 类组件中创建元素引用的推荐现代方式,更安全,也更加明确。
详情
作者的原始描述
Provides exact migration patterns for React string refs (ref="name" + this.refs.name) to React.createRef() in class components. Use this skill whenever migrating string ref usage - including single element refs, multiple refs in a component, refs in lists, callback refs, and refs passed to child components. Always use this skill before writing any ref migration code - the multiple-refs-in-list pat
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7388
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。