我正在尝试克隆子组件,并向其添加引用:
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()?
。
有人知道我能做什么来解决这个问题吗?