Sub topic 15
Typing components that forward refs
This chapter assumes you know useRef for DOM nodes, ComponentPropsWithoutRef, and basic generics. The focus is how forwardRef takes two type parameters and what each one means.
Why forward refs
A ref is not a normal prop. When a parent writes <TextInput ref={inputRef} />, React does not pass ref into the component's props. A plain function component therefore cannot hand the parent a handle to its inner <input>. forwardRef wraps the component so React passes the ref as a second argument.
The two type parameters
The order of the type parameters is the main thing to remember. The first is the type of the ref target. The second is the type of the props.
import { forwardRef, type ComponentPropsWithoutRef } from "react";
export const TextInput = forwardRef<
HTMLInputElement,
ComponentPropsWithoutRef<"input">
>(function TextInput(props, ref) {
return <input ref={ref} {...props} />;
});HTMLInputElementis whatref.currentwill hold once the DOM node mounts. Inside the component,refhas the typeForwardedRef<HTMLInputElement>.ComponentPropsWithoutRef<"input">gives the component every native input attribute. It leaves outrefon purpose, becauseforwardRefadds the ref type itself. UsingComponentProps<"input">here would declare the ref twice and produce confusing errors.
Passing the inner function a name (function TextInput) rather than an arrow function gives the component a displayName in React DevTools and in error messages.
Using it from a parent
The parent creates a typed ref. The element type must match the first type parameter.
import { useRef } from "react";
import { TextInput } from "./TextInput";
export function SearchBar() {
const inputRef = useRef<HTMLInputElement>(null);
return (
<div>
<TextInput ref={inputRef} placeholder="Search" />
<button onClick={() => inputRef.current?.focus()}>Focus</button>
</div>
);
}inputRef.current is HTMLInputElement | null, so the optional chain is required under strict. If you declared useRef<HTMLDivElement>(null) and passed it to TextInput, the compiler would reject it, because a div ref is not assignable to an input ref.
Adding your own props
Intersect your custom props with the native ones, as you did for Button.
import { forwardRef, type ComponentPropsWithoutRef } from "react";
type FieldProps = ComponentPropsWithoutRef<"input"> & {
label: string;
};
export const Field = forwardRef<HTMLInputElement, FieldProps>(
function Field({ label, id, ...rest }, ref) {
return (
<label>
{label}
<input id={id} ref={ref} {...rest} />
</label>
);
}
);Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Type parameters swapped, props are reported as the wrong type | Props listed first | Put the ref element type first, props second |
| `ref` is declared twice, or the ref type conflicts | Used `ComponentProps` | Use `ComponentPropsWithoutRef` |
| `Property 'focus' does not exist on type 'never'` or similar | Ref typed with the wrong element | Match the element in `useRef<...>` to the first parameter |
| Warning that function components cannot be given refs | Forgot `forwardRef` | Wrap the component |
Exercise
Write the type signature for a forwarded-ref Panel component that renders a <div>, accepts all native div props, and lets the parent call ref.current?.scrollIntoView().
Show answer
export const Panel = forwardRef<HTMLDivElement, ComponentPropsWithoutRef<"div">>(function Panel(props, ref) { return <div ref={ref} {...props} />; }); The first type parameter is HTMLDivElement, the element the ref points to. The second is ComponentPropsWithoutRef<"div">, so the ref type is not declared twice.