Sub topic 11
Passing refs to child components
This chapter assumes you can create a ref with useRef, attach it to a DOM element, and pass props between function components. It builds on those skills to cover refs that cross a component boundary.
Why `ref` is not an ordinary prop
A parent often needs the DOM node inside a child. An example is a custom TextInput that the parent wants to focus. The obvious attempt is to put ref on the component:
<TextInput ref={inputRef} />In React 18 and earlier, this does not work. React treats ref as a special attribute and does not put it in props. The child never sees it, inputRef.current stays null, and the console warns that function components cannot be given refs.
React 19: `ref` as a regular prop
In React 19, function components receive ref like any other prop. Read it and pass it to the element that should be exposed.
import { useRef } from 'react';
function TextInput({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
export function Form() {
const inputRef = useRef(null);
return (
<>
<TextInput ref={inputRef} placeholder="Name" />
<button onClick={() => inputRef.current.focus()}>Focus</button>
</>
);
}After mounting, React sets inputRef.current to the <input> node. The parent can then call DOM methods on it.
React 18 and earlier: `forwardRef`
On older versions, wrap the child in forwardRef. React then passes the ref as a second argument, after props.
import { forwardRef, useRef } from 'react';
const TextInput = forwardRef(function TextInput(props, ref) {
return <input ref={ref} {...props} />;
});
export function Form() {
const inputRef = useRef(null);
return (
<>
<TextInput ref={inputRef} placeholder="Name" />
<button onClick={() => inputRef.current.focus()}>Focus</button>
</>
);
}The parent code is the same in both versions. Only the child's definition changes. In React 19, forwardRef still works but is no longer needed for new code.
What the parent receives
The ref points to whatever element the child attaches it to. The child decides what to expose, and the parent cannot reach past that element.
If you forward a ref to a wrapper <div>, the parent gets the div, not the input inside it. Forward it to the element the parent actually needs.
Forwarding hands the parent the full DOM node, so the parent can call any method on it or change its attributes. To expose only a few operations, such as focus, use useImperativeHandle. A later chapter covers it.
Common mistakes
- Reading the ref too early.
ref.currentisnullduring the first render. Read it in event handlers or effects, which run after React attaches the node. - Forgetting to pass the ref on. In React 19, a child that destructures
refbut never puts it on an element leaves the parent's ref asnull. WithforwardRef, ignoring the second argument has the same effect. - Spreading props over the ref. In React 19,
{...props}includesref. Place it deliberately so a later spread does not override it. - Forwarding to a component that cannot take a ref. Each component in the chain must pass the ref down until it reaches a DOM element.
Exercise
A parent renders <Search ref={searchRef} /> on React 18, and searchRef.current is always null. Search is a plain function component that renders an <input>. What two changes fix it?
Show answer
Wrap Search in forwardRef so it receives the ref as a second argument. Then pass that argument to the <input> with ref={ref}. After mounting, searchRef.current is the input node. On React 19 you can instead read ref from props and pass it to the <input>.