Skip to content

Lesson 3 of 14 · 16m

Refs, Portals, and the DOM Boundary

useRef is a mutable box that never re-renders — DOM handles, third-party widgets, and portals that escape stacking contexts.

Sometimes the render output isn't the thing you need — you need the actual <input> node so you can call .focus() on it, the <canvas> so a charting library can draw into it, or the scrollable <div> so you can measure scrollHeight. React's declarative model intentionally has no handle for that. useRef is the deliberate hole in the wall: a mutable box that survives re-renders and never causes one.

const inputRef = useRef(null);
// ...
<button onClick={() => inputRef.current.focus()}>Jump to field</button>
<input ref={inputRef} />

React assigns the DOM node to ref.current during the commit — after the DOM exists. Read it in an event handler or an effect and you get a real node. Read it during render and you're back to the bug from the first lesson: render must be pure, and a mutable box is pure nowhere. Concurrent rendering can interrupt and discard a render; a ref written mid-render leaks into a commit that never happened.

What refs are actually for

  • DOM handles: focus(), scrollIntoView(), getBoundingClientRect() for measurement, scrollTo for virtualized lists.
  • Third-party widgets: a Leaflet map, a CodeMirror editor, a chart canvas. Mount it in an effect against the ref'd node, tear it down in the cleanup. The library owns the DOM inside that node; React owns the node itself. Don't let React re-render children into territory another library is managing.
  • Non-reactive values: interval and timeout IDs, AbortControllers, WebSocket handles, the previous value of a prop. Values that must persist across renders but must not trigger one.

The wrong mental model is "refs are a second kind of state." They're the opposite of state — state drives render, refs opt out of it. If you're storing data in a ref because "the UI doesn't need to update when it changes," that's legitimate; if you're reading it in JSX to avoid a re-render, you've built a cache React can't see.

ref as a prop, and portals

React 19 made forwardRef legacy: function components take ref as a normal prop now, which kills a whole category of wrapper boilerplate that existed only to forward one argument:

// React 19 — ref is just a prop, no wrapper needed
function Input({ ref, ...props }) {
  return <input ref={ref} {...props} />;
}

If a library exposes its own imperative API — a child's scrollToBottom() — useImperativeHandle defines it, but reach for it after callback props fail, not before.

createPortal is the companion API for the other DOM problem: a modal or toast that must escape overflow: hidden clipping and stacking-context traps. The node renders under document.body, but events still bubble through the React tree, not the DOM tree — a click inside the portal propagates to the React ancestors of the component that created it. That's not a bug to be surprised by; it's the documented behavior, and it's usually what you want.

createPortal(
  <Modal onClose={close} />,
  document.body   // DOM parent, not React parent
);

The third-party-widget pattern is the one worth memorizing, because every real app has a map or an editor: mount the library in an effect targeting ref.current, return a cleanup that destroys it, and give the widget's container no React children — React re-rendering into DOM another library manages is how you get half-destroyed maps and detached event listeners.

Field notes

  • The ref callback fires on mount and cleanup (ref={node => ...}); use it when attaching logic to the node itself is cleaner than useRef + effect.
  • useImperativeHandle + ref is the pattern for real form primitives — a custom input that must respond to .focus() from a parent.
  • If every escape hatch in a feature is a ref, the design is fighting React, not using it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.