警告: 函数组件 cannot 有字符串 refs
错误信息
函数组件 cannot 有字符串 refs. 我们 recommend using useRef() 相反.
快速摘要
你 used 字符串 ref (e.g., `ref="myRef"`) on 函数组件, 哪个是 legacy API 不 supported in 函数组件.
为什么发生
Used
ref="myInput"(字符串 ref) 相反的ref={myRef}(对象 ref)Tried 到通过 ref 到函数组件不使用 forwardRef
最小示例
✗ 错误代码
<input ref="myInput" /> // String ref!✓ 修复代码
const myInput = useRef(null);
// ...
<input ref={myInput} />字符串 refs 是 deprecated. useRef 创建对象 ref 那个工作使用函数组件 and Hook.
如何诊断
Look for 字符串 refs:
ref="某事"检查如果你're passing ref 到函数组件
如何修复
Use useRef:
const myRef = useRef()andref={myRef}Use React.forwardRef 到通过 refs 到函数组件
如何预防
从不 use 字符串 refs — 总是 use useRef
Use forwardRef 当函数组件需要到 expose ref