无法将ref添加到克隆的元素

时间:2020-02-10 21:50:06

标签: reactjs

我正在尝试克隆子组件,并向其添加引用:

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()?

有人知道我能做什么来解决这个问题吗?

0 个答案:

没有答案