Common forwardRef Use Cases
You'll learn: Where forwardRef shines in real-world component libraries and design systems.
The Problem It Solves
You've learned how forwardRef works. Now let's see when you actually need it. The pattern emerges most often when building reusable UI components that wrap native elements but still need to expose direct DOM access to parent components.
The Three Main Use Cases
1. Custom Input Wrappers
The most common scenario: you build a <TextInput> component with labels, error messages, and styling — but parents still need to call .focus().
const TextInput = forwardRef<HTMLInputElement, Props>((props, ref) => {
return (
<div className="input-wrapper">
<label>{props.label}</label>
<input ref={ref} type="text" {...props} />
{props.error && <span>{props.error}</span>}
</div>
);
});
// Parent can now focus it
function Form() {
const inputRef = useRef<HTMLInputElement>(null);
return <TextInput ref={inputRef} label="Name" />;
}
2. Custom Button Components
Design systems need styled buttons that still support ref-based operations like measuring or scrolling into view.
const Button = forwardRef<HTMLButtonElement, Props>((props, ref) => {
return <button ref={ref} className="fancy-button" {...props} />;
});
3. Reusable Containers and Cards
Any wrapper component that parents might need to scroll to, measure, or manipulate.
const Card = forwardRef<HTMLDivElement, Props>((props, ref) => {
return <div ref={ref} className="card">{props.children}</div>;
});
When You Don't Need It
If your component never wraps a DOM element or parents will never need direct access, skip forwardRef. Simple presentational components rarely need it.
Key Takeaway: Use forwardRef when building component library abstractions (inputs, buttons, containers) that wrap DOM elements but must stay transparent to ref-based operations from parent components.