Saturday, 23 November 2019

pass forwardRef via cloneElement reactjs

const ref = useRef()  

 React.Children.map(this.props.children, (element) => {
   React.cloneElement(element, { 
     innerRef: node => ref,
   })
 })

here element is a component

like the following

const newComponent = forwardRef(({children, ...otherprops}, ref){
    return (
     <div {...otherprops} ref={otherprops.innerRef}>
       {children}
     </div>
    )        
})

getting ref is null in forwardRef...

Reproducible example :- https://codesandbox.io/s/forward-ref-cloneelement-1msjp



from pass forwardRef via cloneElement reactjs

No comments:

Post a Comment