const ref = useRef()
React.Children.map(this.props.children, (element) => {
React.cloneElement(element, {
innerRef: node => ref,
})
})
这里元素是一个组件
像下面这样
const newComponent = forwardRef(({children, ...otherprops}, ref){
return (
<div {...otherprops} ref={otherprops.innerRef}>
{children}
</div>
)
})
得到ref是null在 forwardRef ...
可重现的示例:- https://codesandbox.io/s/forward-ref-cloneelement-1msjp
哆啦的时光机
相关分类