forwardRef: Exposing DOM Refs to Parents
Learn how forwardRef lets parent components access a child's DOM node or component instance.
forwardRef: Exposing DOM Refs to Parents is a free React Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Refs Are and the Default Limitation
A ref is a way to hold a reference to a DOM node or component instance that persists across renders without triggering re-renders. You attach one with the ref attribute, and React fills in its current property after committing to the DOM.
The catch is that you cannot attach a ref to a function component the way you would a div. By default a parent has no path to reach the DOM node living inside a child component, because refs are not passed down like ordinary props.
forwardRef Wraps a Component
React.forwardRef lets a component receive a ref from its parent and forward it onward to one of its own elements. It wraps your component function and changes the function signature so that ref arrives as a second argument alongside props.
This is the official escape hatch for exposing an inner DOM node to a calling component, turning an otherwise opaque child into one whose element a parent can directly reference.
The forwardRef Signature
The wrapped function takes two parameters: (props, ref). The first is the usual props object, and the second is the forwarded ref. You then spread or attach that ref onto the element you want to expose, such as ref={ref} on an inner input.
Remember that only forwardRef-wrapped components receive this second argument. A plain function component that lists ref as a second parameter will simply receive undefined.
Use Case: Focusing a Child Input from the Parent
The classic example is a reusable text field component whose inner input you want a parent to focus. By wrapping the field in forwardRef and attaching the forwarded ref to the underlying input element, the parent can call focus on it directly.
This keeps the field component reusable while granting controlled imperative access exactly where it is needed, like focusing the first field of a form on mount.
Measuring a Child Element
Forwarded refs also enable measurement. A parent can read offsetWidth, offsetHeight, or call getBoundingClientRect on a child element it received through forwardRef.
This is useful for tooltips, popovers, and layout calculations where the parent must know the exact rendered size or position of an inner element before deciding how to place something relative to it.
Calling Scroll Methods
Because the forwarded ref points at a real DOM node, the parent can invoke native DOM methods on it. Calling scrollIntoView on a forwarded list item smoothly brings it into view, and scrollTo on a forwarded scroll container repositions its content.
These are imperative actions that have no clean declarative equivalent, which is exactly the kind of interaction refs are meant to handle.
Passing a Ref Through Multiple Layers
Sometimes the element you want to expose is buried several components deep. You can chain forwardRef so each intermediate layer forwards the ref it receives down to the next, until it finally lands on the target DOM node.
Each wrapper in the chain must itself use forwardRef and pass the ref along, since an ordinary component in the middle would break the connection.
forwardRef + memo Composition
forwardRef and React.memo can be combined to get both ref forwarding and render memoization. The usual order is memo(forwardRef(Component)), wrapping the forwardRef result so the memo comparison still applies.
This pattern appears in performance-sensitive design systems where a frequently rendered component must both expose its node and skip needless re-renders.
Viewing forwardRef in DevTools
In React DevTools a forwardRef component shows up with a ForwardRef label wrapping your component name. This makes it easy to confirm at a glance that a component participates in ref forwarding.
You can give the inner function a name or set a displayName to make the DevTools entry clearer than a generic anonymous ForwardRef label.
When NOT to Use forwardRef
forwardRef is for exposing DOM nodes and imperative handles, not for passing data. If a parent needs to influence a child, prefer ordinary props and state rather than reaching in through a ref.
Overusing refs couples components tightly and bypasses React data flow, making behavior harder to follow. Reach for forwardRef only when a declarative prop genuinely cannot express the interaction.
forwardRef in Modern React
forwardRef remains the standard tool for exposing refs in component libraries and reusable inputs. Newer React versions are moving toward letting ref be passed as a regular prop, but forwardRef is still widely used and important to understand.
Knowing it well prepares you for both existing codebases and the underlying mechanics that any future ref-as-prop syntax builds upon.
Quick Check: forwardRef Signature
Test your understanding of how a forwardRef component receives its ref.
Recap: forwardRef
forwardRef solves the limitation that a parent cannot reach a child component DOM node by giving the component a second ref argument it can forward onto an inner element. This enables focusing, measuring, and scrolling child elements.
It composes with memo, can be chained through layers, and shows as ForwardRef in DevTools. Use it for imperative DOM access, not for passing data, which props handle better.
Frequently asked questions
Is the “forwardRef: Exposing DOM Refs to Parents” lesson free?
Yes — the full text of “forwardRef: Exposing DOM Refs to Parents” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “forwardRef: Exposing DOM Refs to Parents”?
Learn how forwardRef lets parent components access a child's DOM node or component instance. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “forwardRef: Exposing DOM Refs to Parents” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- forwardRef: Exposing DOM Refs to Parents
- useImperativeHandle: Custom Instance Values
- Building an Imperative Component API
- When to Use Imperative vs Declarative APIs