日本語

How to Receive a ref Without forwardRef in React 19

​​React​forwardRef,ref,TypeScript,useImperativeHandle

This article was translated from the Japanese original with AI. 日本語の原文を読む

In React 19, function components can receive ref the same way they receive other props. Because of this, components you write from now on no longer need to be wrapped in forwardRef. forwardRef itself still works in React 19, and it is planned to be deprecated in a future version.

In my test environment, I ran the same TypeScript (.tsx) component on both React 19 and React 18, and checked where a ref passed from the parent actually ends up. Below I summarize those results and the changes described in the official documentation.

Note

The test environment was Ubuntu 24.04 and Node.js 22.22.0. On the React 19 side, I used react / react-dom 19.3.1, @types/react 19.3.0, and typescript 7.0.2. The React 18 side used react / react-dom 18.3.1. Both were transpiled with esbuild 0.28.2 and rendered on jsdom 29.1.1. I type-checked the .tsx files with tsc. The GIF was recorded in Chromium with Playwright 1.56.1.

In React 19, you just read ref from props

According to the React 19 announcement, starting in React 19 you can read ref as a prop in function components. For the test, I prepared the following MyInput. It does not use forwardRef; it just destructures ref and passes it to <input>.

tsx
import type { Ref } from 'react';

type Props = { placeholder?: string; ref?: Ref<HTMLInputElement> };

export function MyInput({ placeholder, ref }: Props) {
  return <input placeholder={placeholder} ref={ref} />;
}

I rendered it from a parent that passes a ref, and checked the contents of ref.current and whether calling ref.current.focus() moved focus to the input. I also transpiled and ran the same file on React 18.

text
react 19.3.1
MyInput: ref.current=<input> focused=true
text
react 18.3.1
MyInput: ref.current=null focused=false
  console.error: Warning: MyInput: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)
  console.error: Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

On React 19, ref.current was the <input> and focus moved there. On React 18, ref.current stayed null, and two warnings were printed: "ref is not a prop" and "Function components cannot be given refs. … Did you mean to use React.forwardRef()?"

Here is the same MyInput running in Chromium, where I pressed a button three times to call ref.current.focus(). The top is React 18 and the bottom is React 19.

Screen showing MyInput on React 18 and 19 stacked vertically, with ref.current and the focus count after pressing the focus button three times
Three presses of the focus button. On React 18, ref.current stays null and the count stays at 0. On React 19, focus moves to the input field and the count reaches 3

forwardRef still works in React 19

The previous approach was to receive ref as the second argument of forwardRef.

tsx
import { forwardRef } from 'react';

type Props = { placeholder?: string };

export const MyInputForwardRef = forwardRef<HTMLInputElement, Props>(
  function MyInputForwardRef({ placeholder }, ref) {
    return <input placeholder={placeholder} ref={ref} />;
  },
);

This component produced ref.current=<input> focused=true on both React 19.3.1 and React 18.3.1. Running it in React 19.3.1's development build did not print any warning about using forwardRef.

The beginning of the forwardRef reference says that in React 19, forwardRef is no longer needed and that you should pass ref as a prop instead, and that forwardRef will be deprecated in a future release. The announcement also says it will be deprecated and eventually removed in a future version. Code will not break if you don't rewrite it right away, but the recommendation is not to use it in new components.

For rewriting, the official react-codemod includes a codemod named remove-forward-ref. The README gives the command npx codemod react/19/remove-forward-ref --target <path>. In my test environment, I could not fetch the package from the codemod registry, so I could not run it. As a result, I have not verified its output.

Point

In React 18, the form that receives ref as a prop does not work. For components from libraries that also support React 18, you still need to keep forwardRef.

useImperativeHandle and memo also receive ref as a prop

When you want to expose only chosen methods to the parent rather than the DOM node itself, use useImperativeHandle. The useImperativeHandle reference now shows passing the ref received as a prop directly.

tsx
import { useImperativeHandle, useRef, type Ref } from 'react';

export type FancyInputHandle = { focus: () => void; clear: () => void };

export function FancyInput({ ref }: { ref?: Ref<FancyInputHandle> }) {
  const inputRef = useRef<HTMLInputElement>(null);
  useImperativeHandle(ref, () => ({
    focus: () => inputRef.current?.focus(),
    clear: () => {
      if (inputRef.current) inputRef.current.value = '';
    },
  }), []);
  return <input ref={inputRef} defaultValue="hello" />;
}

On React 19, the parent's ref.current became the { focus, clear } object, calling focus() moved focus to the input, and calling clear() emptied it. On React 18, ref.current was null.

I also tested a component wrapped in memo (memo(function MemoInput({ placeholder, ref }: Props) { ... })). On React 19, ref.current was <input>, while on React 18 it was null. Even when wrapped in memo, on React 19 ref still reaches the inner component as a prop.

In TypeScript, declare ref in the props type

When a function component receives ref, write it in the props type, such as ref?: Ref<HTMLInputElement>. If you use ComponentProps<'input'> to receive all the <input> props together, ref is already included in the type without adding it yourself. In my test, a component that took props: ComponentProps<'input'> and passed them to <input {...props} /> produced ref.current=<input> on React 19.

I also checked what happens when you pass a ref to a component that does not accept one. On React 19.3.1, ref.current stayed null, and no warning was printed. Unlike React 18, which printed "Function components cannot be given refs" for MyInput, there is no runtime warning here.

On the other hand, tsc catches this mistake. The line that passed a ref to a component whose type does not declare ref (line 13) was an error. The line that passed a <div> ref to MyInput (line 14), and the line that passed it to a component receiving ComponentProps<'input'> (line 15), were also errors. The error on line 15 also shows that the ref in ComponentProps<'input'> is typed for <input>. The following output omits the three-line follow-up explanations for the errors on lines 14 and 15.

text
src/TypeError.tsx(13,19): error TS2322: Type '{ ref: RefObject<HTMLInputElement | null>; }' is not assignable to type 'IntrinsicAttributes & { placeholder?: string | undefined; }'.
  Property 'ref' does not exist on type 'IntrinsicAttributes & { placeholder?: string | undefined; }'.
src/TypeError.tsx(14,16): error TS2322: Type 'RefObject<HTMLDivElement | null>' is not assignable to type 'Ref<HTMLInputElement> | undefined'.
src/TypeError.tsx(15,22): error TS2322: Type 'RefObject<HTMLDivElement | null>' is not assignable to type 'Ref<HTMLInputElement> | undefined'.
Warning

On React 19.3.1, passing a ref to a component that does not use it did not print any runtime warning. Because ref.current just stays null and the problem is easy to miss, it is safer to declare ref in the props type and let tsc check it.

Class components are unchanged, and element.ref is deprecated

Class components work as before. As the note in the announcement says, a ref passed to a class refers to the instance, so it does not appear in props. In my test, on both React 19 and React 18 the parent's ref.current was the class instance, and this.props.ref was undefined.

One thing that did change is reading ref from a JSX element object. According to the React 19 upgrade guide, element.ref is deprecated, and you should use element.props.ref instead. In my test on React 19, the ref passed to the element was present in element.props.ref. element.ref also returned the same ref, but reading it logged this error:

text
console.error: Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.

On React 18, element.props.ref did not contain the ref, and only element.ref returned it. Code that inspects child elements and pulls out ref needs to be changed to use element.props.ref on React 19.

Is forwardRef no longer needed?

Here is what the tests and the official documentation show, split by case.

Case forwardRef
New function component written for React 19 Not needed. Receive ref as a prop
Existing component upgraded to React 19 Still works. You may rewrite it to prepare for the future deprecation
Component still used with React 18 Still needed. On React 18, ref does not arrive as a prop
Class component Never used. ref refers to the instance

If you only target React 19, you no longer need to write forwardRef. When rewriting, the approach my tests support as safe is to add ref to the props type and use tsc to check that no ref is passed to the wrong place.

​Posted by 86