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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- React 19:
function TextField({ ref, ...props }) { return <input ref={ref} {...props} /> }works.forwardRefstill 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.refis deprecated; React readselement.props.ref.- React 19.3 (stable):
<Fragment ref={ref}>gives aFragmentInstancewithaddEventListener,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 19refis a prop; on 18 I would useforwardRef."
The red pen
Common traps: mistake → fix
Mistake: "
forwardRefis required in React 19"Fix: ref is a normal prop for function components now.
Mistake: "
forwardRefwas 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
- Our
<TextField>needsfocus()and<ChatList>needsscrollToBottom(). How do you expose them in React 19? - What changed from React 18? Is
forwardRefremoved? - Why limit the handle instead of exposing the DOM node?
- 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 voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.