DEV Community

Discussion on: How to use React forwardRef

Collapse
 
z2lai profile image
z2lai • Edited

I've implemented my ref forwarding with useRef hook and function components the same way you did, but I'm still getting inputRef.current is undefined and the warning "Function components cannot be given refs."

According to React docs, you need to implement ref forwarding using the useImperativeHandle hook: reactjs.org/docs/hooks-reference.h...

Collapse
 
renatobentorocha profile image
Renato Rocha

Hello z2lai, thanks by comment.

I implemented the example in this post for you to see: github.com/renatobentorocha/forwar....

The useRef hook is used to persist an object reference even after a component to be rerendered: reactjs.org/docs/hooks-reference.h...

Collapse
 
z2lai profile image
z2lai

Hey Renato, thanks so much for the response! Your example convinced me that there was something else that was wrong in my app. It turns out that my "forwardRef(Input);" was wrapped in another HOC that was not passing in ref as the extra second argument. So I removed the HOC and now it works, thanks a lot for your help!