More React answers

by Danny
Published 2 min read

In React 19, function components receive ref as an ordinary prop, so you can drop forwardRef. Learn how to migrate, type it in TypeScript and avoid the common traps.

The short answer

#

From React 19, a function component receives ref as a normal prop. Remove the forwardRef wrapper and read ref from props like any other value. forwardRef still works for now, but the React docs say it will be deprecated in a future release.


_10
-const TextInput = forwardRef(function TextInput(props, ref) {
_10
- return <input ref={ref} {...props} />;
_10
-});
_10
+function TextInput({ ref, ...props }) {
_10
+ return <input ref={ref} {...props} />;
_10
+}

Why it happens

#

Before React 19, ref was a reserved name, just like key. React removed it from the props object before your component ran, so a component that wanted to pass a ref down to a DOM element had no way to see it. forwardRef was the workaround: it gave your component a second argument that held the ref.

React 19 stops treating ref specially for function components. It now arrives in props, so the extra wrapper is unnecessary. A few related changes came with it:

  • key is still reserved and is not passed as a prop.
  • Class components are unchanged. A ref on a class component still points to the class instance.
  • Reading element.ref on a JSX element is deprecated; the value now lives in element.props.ref.

How to fix it

#

Read ref from props


_14
function SearchBox({ ref, placeholder }) {
_14
return <input ref={ref} type="search" placeholder={placeholder} />;
_14
}
_14
_14
function Toolbar() {
_14
const inputRef = useRef(null);
_14
_14
return (
_14
<>
_14
<SearchBox ref={inputRef} placeholder="Search" />
_14
<button onClick={() => inputRef.current?.focus()}>Focus</button>
_14
</>
_14
);
_14
}

useImperativeHandle works the same way. Pass it the ref prop instead of the second forwardRef argument.

Type it in TypeScript

Add ref to your props type. If your component wraps a DOM element, React.ComponentProps for that element already includes it:


_12
type TextInputProps = React.ComponentProps<'input'> & {
_12
label: string;
_12
};
_12
_12
function TextInput({ label, ref, ...props }: TextInputProps) {
_12
return (
_12
<label>
_12
{label}
_12
<input ref={ref} {...props} />
_12
</label>
_12
);
_12
}

For a custom shape, declare it yourself with ref?: React.Ref<HTMLInputElement>. Make sure @types/react is on version 19 so the types match the runtime.

Run the codemod

The React team publishes a codemod that removes forwardRef and moves the ref into props:


_10
npx codemod@latest react/19/remove-forward-ref --target src

Review the diff afterwards, especially components that combine forwardRef with memo or generics.

Keep forwardRef if you support React 18

If you publish a component library that still supports React 18, keep forwardRef for now. In React 18, ref is still stripped from props, so the new pattern would silently receive undefined.

Common pitfalls

#
  • Keeping a custom prop such as innerRef or inputRef purely to work around the old limitation. You can now call it ref, which is what users of your component expect.
  • Forgetting memo. Instead of memo(forwardRef(...)), write memo(function Input({ ref, ...props }) ...); the ref is passed through.
  • Implicit returns in callback refs. React 19 lets a callback ref return a cleanup function, so TypeScript now rejects arrow functions that return a value, such as ref={(node) => (current = node)}. Use a block body: ref={(node) => { current = node; }}.
  • Expecting class components to change. They still need the instance ref, and you cannot read ref from their props.
  • Mixing versions. If React and @types/react are on different major versions, you get confusing type errors about ref.
#

Was this resource helpful?