← Back to React, page 4

react-0079 · Medium mid

How do you expose a child's DOM node or methods to a parent in React 19, and what changed about forwardRef?

In React 19, function components receive ref as an ordinary prop, so forwardRef is no longer needed: function Input({ ref, ...props }) { return <input ref={ref} {...props} /> }. forwardRef still works but is deprecated and will be removed later. To expose a limited API instead of the raw node, call useImperativeHandle(ref, () => ({ focus, clear })), so the parent can call methods without reaching into the DOM.

Full answer

A ref normally attaches to a DOM node or class instance. Passing ref to your own function component did not work before React 19, because ref was not a prop. forwardRef was the wrapper that received it as a second argument.

In React 19 for function components, ref is a regular prop: function Input({ ref, ...props }). Pass it down to a DOM element and the parent's ref points to that element. Code that used forwardRef continues to work, and React ships codemods to remove it. forwardRef is expected to be deprecated and removed in a future version. Class components do not receive ref as a prop, because it refers to the class instance.

Often you do not want to expose the entire DOM node. useImperativeHandle(ref, createHandle, deps) lets the child decide what the parent gets: an object such as { focus() {}, clear() {} }. The parent writes inputRef.current.focus() and cannot touch the other node properties. This keeps the child's internals private and the API small. Pass dependencies when the handle uses changing values.

Use imperative handles sparingly, for things that are naturally commands: focus, scroll to, play, pause, reset. Anything that can be expressed with props and state should be, because imperative code is harder to follow. Inputs that need clear are usually better as controlled components with the parent owning the value.

React 19 also lets a ref callback return a cleanup function, which runs when the element is detached, replacing the pattern of calling the callback with null. The TypeScript type for a ref prop is Ref<HTMLInputElement>, or use ComponentProps<'input'> that already includes it.

The example exposes focus and clear.

jsx
import { useImperativeHandle, useRef } from 'react';

function CouponInput({ ref, ...props }) {
  const inputRef = useRef(null);

  useImperativeHandle(ref, () => ({
    focus() {
      inputRef.current.focus();
    },
    clear() {
      inputRef.current.value = '';
    },
  }));

  return <input ref={inputRef} {...props} />;
}

export default function App() {
  const couponRef = useRef(null);

  return (
    <div>
      <CouponInput ref={couponRef} aria-label="Coupon" />
      <button onClick={() => couponRef.current.focus()}>Focus</button>
      <button onClick={() => couponRef.current.clear()}>Clear</button>
    </div>
  );
}

The parent only sees focus and clear, not the input element. The check set a value, pressed Focus, and then pressed Clear. Output of the run: after Focus: input focused = true after Clear: input value = ""

Key points

  • In React 19, ref is a regular prop for function components, so forwardRef is not needed.
  • forwardRef still works for now and is slated for removal.
  • useImperativeHandle exposes a small custom API instead of the raw DOM node.
  • Use imperative handles for commands, and props and state for everything else.
  • Ref callbacks can return a cleanup function in React 19.

Common mistakes

  • Still wrapping every component in forwardRef in new React 19 code.
  • Exposing the whole DOM node when a small API is enough.
  • Using imperative methods for things that props and state can express.
  • Forgetting that class components do not take ref as a prop.
  • Reading ref.current during render.

Follow-ups

  • How do you type a ref prop in TypeScript?
  • What does a ref callback cleanup function do in React 19?
  • When is useImperativeHandle a code smell?

Tips

  • State the React 19 change first, then the legacy form. Interviewers still ask about forwardRef.
  • Show you prefer props over imperative handles.
  • Give focus and scroll as the good use cases.