Search code examples
reactjsreact-ref

Passing a React ref to deeply nested child components


I need to controle a ref (React useRef) from the parent component. The ref is in a deeply nested child component.

Do I have to use forwardRef in every child component, till I reach the HTML element where the ref is defined?


Solution

  • You can use a context to directly pass your ref.

    You already have a detailed explanation in this question. => How do I pass ref to a neighbour component