How to assign ref in forwardRef component
How to assign multiple refs to a react component wrapped with forwardRef. Copy-paste solution.
Context
I had a component that I wanted to pass the ref
prop into. To do that with React, you have to use React.forwardRef
function and pass the second argument to the appropriate child component. It would look like this:
And now, what if I wanted to access that ref
inside the YourComponent
. Normally I would try to access it through ref.current
, but here it's different. The ref
variable (forwardRef's second parameter) doesn't have .current property since it is a function. A function that takes HTMLDivElement
as an argument.
Solution
In such a case, what I need to do is:
- create a new ref inside
YourComponent
, let's call ittestRef
, - in
Wrapper
component, add lambda function as a value ofref
prop. With HTMLDivElement as a parameter, - assign the value of a lambda's parameter to
testRef.current
- call the
ref(el: HTMLDivElement | null)
and pass the lambda's parameter as an argument.
This would look like this:
but it doesn't look the best! We could create a simple utility class for that or.... steal it from StackOverflow from the author you don't remember and can't find at the time of writing a blog post!
and the final YourComponent's form is...
Published on June 16, 2022 • 2 min read