Forward a DOM ref
Legacy or compatibility code needs a function component to accept a parent ref and attach it to a descendant DOM element.
React 19+ · Function
forwardRef lets a component expose a child DOM node or imperative handle through a parent ref. In React 19, it is no longer necessary for function components because ref can be received directly as a prop, and it will be deprecated in a future release.
forwardRef accepts a render function and returns a component that can receive a ref. The render function receives the parent’s props and ref, then normally attaches that ref to a child element or passes it to useImperativeHandle. React 19 makes this wrapper unnecessary for function components because ref can be received directly as a prop.
const SomeComponent = forwardRef(render)| Name | Type | Description |
|---|---|---|
render | function | The render function for the component. React calls it with the received props and ref, and its returned JSX becomes the component output. |
A React component that can receive a ref prop and render the result of the supplied function.
import { useRef } from 'react';
function MyInput({ ref, placeholder }: {
ref: React.Ref<HTMLInputElement>;
placeholder: string;
}) {
return <input ref={ref} placeholder={placeholder} />;
}
export default function Form() {
const inputRef = useRef<HTMLInputElement>(null);
return (
<>
<MyInput ref={inputRef} placeholder="Name" />
<button onClick={() => inputRef.current?.focus()}>Focus</button>
</>
);
}In React 19, pass ref directly as a prop to a function component instead of wrapping it with forwardRef.
Legacy or compatibility code needs a function component to accept a parent ref and attach it to a descendant DOM element.
A component exposes methods or a custom value rather than its underlying DOM node.
A React 18-compatible component must accept refs before migrating to React 19’s direct ref prop.
forwardRef(render) takes a render function and returns a React component that can receive a ref prop. React invokes render with the component props and the parent-provided ref.
Follow-up: How should the render function use the ref? Pass it to a child component or give it to useImperativeHandle to expose a chosen value.
The render function receives two arguments: props and ref. The ref may be an object or a callback, and it is null when the parent did not provide one.
Follow-up: What should a component do when the ref is null? It should remain valid without assuming that a parent supplied a ref.
React 19 lets function components receive ref directly as a prop, so forwardRef is no longer necessary for new components. The documentation says forwardRef will be deprecated in a future release.
Follow-up: How would you modernize an existing wrapper? Move ref into the component’s props and attach it directly to the target element.
useImperativeHandleuseRefmemo