What is forwardRef?
forwardRef wraps a function component so it can receive a ref from its parent and pass it to a child element, typically a DOM node. It was required up to React 18. In React 19, function components receive ref as a regular prop, so forwardRef is no longer needed and the React team plans to deprecate and remove it.
React 18 and earlier
const Input = forwardRef<HTMLInputElement, Props>((props, ref) => <input ref={ref} {...props} />). Without it, ref was stripped from props and never reached the component.
React 19
function Input({ ref, ...props }) { return <input ref={ref} {...props} /> }.- Works with
useImperativeHandlethe same way. - A codemod converts
forwardRefusage. forwardRefstill works in 19 for compatibility; libraries that support React 18 still use it.- Class components still receive refs to their instance, not as a prop.
Why expose refs
Reusable inputs, buttons and wrappers in design systems need to let parents focus them, measure them, or let libraries (popovers, form libraries such as React Hook Form's register) attach to the underlying DOM node.
Related React 19 ref changes
- Ref callbacks can return a cleanup function.
element.refis deprecated; refs are props.- String refs were removed.
useRefrequires an argument in TypeScript types (useRef(null)).
Code example
1import { forwardRef, useRef } from "react";23// React 18 style (still works, but unnecessary in React 19)4export const OldInput = forwardRef<HTMLInputElement, React.ComponentProps<"input">>(function OldInput(props, ref) {5 return <input ref={ref} className="input" {...props} />;6});78// React 19 style: ref is just a prop9export function Input({ ref, ...props }: React.ComponentProps<"input">) {10 return <input ref={ref} className="input" {...props} />;11}1213export function SearchBar() {14 const inputRef = useRef<HTMLInputElement>(null);15 return (16 <div>17 <Input ref={inputRef} placeholder="Search" />18 <button onClick={() => inputRef.current?.focus()}>Focus search</button>19 </div>20 );21}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.