Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React forwardRef: How deep can you pass down forwardRef?

I'm wondering how deep you can pass forwardRef down in a nested app, and how would someone pass it down?

Something like this:

<Parent>
  <Child>
    ....
  </Child>
</Parent>

    <Child>
      <GrandChild>
          ....      
      </GrandChild>
    </Child>

        <GrandChild>
          <GreatGrandChild>
            ....
          </GreatGrandChild>
        </GrandChild>

            <GreatGrandChild>
              ....
            </GreatGrandChild>

and then use it in GreatGrandChild component or passing down the ref of and element in Parent down to GreatGrandChild

like image 976
Misuti Avatar asked Jan 30 '26 10:01

Misuti


1 Answers

You could pass a ref as deep as you want but that seems inefficient.

One alternative would be to use the context API to set a ref in the producer (component containing the ref element) and use it in the consumer (child that will utilize the ref)

like image 54
varoons Avatar answered Feb 02 '26 02:02

varoons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!