forwardRef in React
forwardRef lets a function component receive a ref from its parent and attach it to something inside, usually a DOM node. Before React 19, ref wasn’t a normal prop, so this wrapper was the only way. In React 19, function components can take ref as a regular prop, and forwardRef is no longer needed for new code. You still have to know it: most codebases and libraries use it.
1. The problem
function TextInput(props) {
return <input {...props} />;
}
function Form() {
const ref = useRef(null);
// React 18 and earlier: ref is not passed through, ref.current stays null
return <TextInput ref={ref} />;
}
React treated ref (like key) as a special attribute and stripped it from props.
2. The forwardRef solution (React 18 and earlier)
import { forwardRef } from 'react';
const TextInput = forwardRef(function TextInput(props, ref) {
return <input ref={ref} {...props} />;
});
function Form() {
const ref = useRef(null);
return (
<>
<TextInput ref={ref} placeholder="Email" />
<button onClick={() => ref.current.focus()}>Focus</button>
</>
);
}
forwardRef passes the ref as a second argument to your render function.
3. The React 19 way
function TextInput({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
No wrapper. ref arrives in props like anything else.
4. Exposing a limited API with useImperativeHandle
Handing out the raw DOM node lets a parent do anything to it. To expose only specific methods:
import { forwardRef, useImperativeHandle, useRef } from 'react';
const VideoPlayer = forwardRef(function VideoPlayer({ src }, ref) {
const videoRef = useRef(null);
useImperativeHandle(ref, () => ({
play: () => videoRef.current.play(),
pause: () => videoRef.current.pause(),
}), []);
return <video ref={videoRef} src={src} />;
});
// Parent: playerRef.current.play()
Use this sparingly. Props are still the default way to control a child; imperative handles are for things that have no declarative form, such as focus, scroll, play and pause.
5. TypeScript
// forwardRef<RefType, PropsType>: note the order
const TextInput = forwardRef<HTMLInputElement, React.ComponentProps<'input'>>(
function TextInput(props, ref) {
return <input ref={ref} {...props} />;
}
);
// React 19
function TextInput({ ref, ...props }: React.ComponentProps<'input'>) {
return <input ref={ref} {...props} />;
}
6. Gotchas
- DevTools names. Anonymous arrow functions inside
forwardRefshow asForwardRef. Use a named function, as above. - With
memo. Wrap in this order:memo(forwardRef(...)). - Higher-order components. A HOC must forward the ref itself or the ref lands on the wrapper. See higher-order components.
- Class components. A ref on a class component gives you the instance. No forwarding involved.
Choosing an approach
| Situation | Use |
|---|---|
| New code on React 19 | ref as a prop |
| React 18 or earlier, or a library supporting both | forwardRef |
| Parent needs a few specific actions, not the node | useImperativeHandle |
| Parent wants to change what the child shows | Props, not refs |
Follow-up questions to expect
- “Why couldn’t you just pass
refas a prop before?” React reserved it. A common workaround was a custom prop name such asinputRef. - “When is a ref to a child justified?” Focus management, scrolling to an item, measuring, and media controls. Design-system inputs and buttons should always pass refs through.
- “What does
ref.currenthold?” Whatever the child attached it to: a DOM node, or the object returned fromuseImperativeHandle.
Related: useRef in React.
