Search code examples
javascriptreactjsreact-functional-componentuse-ref

How do I forward a ref down to a child component?


The react documentation provides a way for a parent component to get the ref from a child component using the forwardRef method. But, I need to get the width of a parent component in a child component. Is there a way to forward a parent's ref in the child component?


Solution

  • You could just access the ref in parent, make appropriate calculations and send down the props (width in your case) to the child.

    <ChildComp parentWidth={ref.current && calc_for_ref} />