reactjs - 无法将 ref 添加到克隆的元素
问题描述
我正在尝试克隆一个子组件并向它添加一个引用,如下所示:
let targetChild = null;
React.Children.forEach(children, (child, index) => {
if (child.type.displayName === 'TargetChildElement') {
targetChild = React.cloneElement(child, { ref: index });
}
});
但是,当子元素正常工作时出现错误:
Uncaught Error: Function components cannot have refs. Did you mean to use React.forwardRef()?
.
有谁知道我能做些什么来解决这个问题?
解决方案
推荐阅读
- java - 在 Vaadin Flow 中的布局中居中小部件
- c - 传递 'strcmp' 的参数 1 使指针从整数而不进行强制转换
- javascript - 如何将 div 元素的切换设置为首先隐藏并且仅在单击后显示?
- swift - 当我转到另一个视图时,为什么不调用 deinit ?
- javascript - 为什么这些脚本标签在我的 HTML 文件中不起作用?
- python - Kivy:从不同的文件访问类中的应用程序方法
- visual-studio-code - 阻止 VS Code 使用 Apple CoreSimulator?
- python - 关于 Keras 中 LSTM/GRU 中隐藏层的困惑
- azure - 巨大的 Azure 资源清理 - 需要建议
- javascript - 如何修复这个 forEach 函数?