Skip to the page

Refs and imperative code · mid

Ref as a prop, useImperativeHandle and Fragment refs

In React 19 a function component receives ref as a normal prop, so forwardRef is no longer needed; useImperativeHandle exposes a small API (like focus()) instead of the whole DOM node, and React 19.3 adds refs on Fragments.

The notes

What you must know

  • React 19: function TextField({ ref, ...props }) { return <input ref={ref} {...props} /> } works. forwardRef still works but is planned for deprecation (codemod planned).
  • useImperativeHandle(ref, () => ({ focus, scrollToBottom }), []) exposes only the methods you choose; parents cannot reach into the DOM node.
  • Prefer props and state over imperative calls; use imperative handles for focus, scroll, play/pause, measuring, or integrating non-React widgets.
  • Ref callbacks can return a cleanup (React 19).
  • element.ref is deprecated; React reads element.props.ref.
  • React 19.3 (stable): <Fragment ref={ref}> gives a FragmentInstance with addEventListener, focus/focusLast/blur, observeUsing (IntersectionObserver/ResizeObserver), getClientRects, scrollIntoView, for a group of children without a wrapper div.
  • Interviewers who learned React 18 may still expect forwardRef; say both: "In React 19 ref is a prop; on 18 I would use forwardRef."

The red pen

Common traps: mistake → fix

  • Mistake: "forwardRef is required in React 19"

    Fix: ref is a normal prop for function components now.

  • Mistake: "forwardRef was removed"

    Fix: it still works; deprecation is planned.

  • Mistake: "Expose the whole DOM node as the public API"

    Fix: expose a minimal handle with useImperativeHandle.

Easy to hard

Questions interviewers ask, easy to hard

  1. Our <TextField> needs focus() and <ChatList> needs scrollToBottom(). How do you expose them in React 19?
  2. What changed from React 18? Is forwardRef removed?
  3. Why limit the handle instead of exposing the DOM node?
  4. When would you use a Fragment ref (19.3)?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview