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,scrollTofor 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
refcallback fires on mount and cleanup (ref={node => ...}); use it when attaching logic to the node itself is cleaner thanuseRef+ effect. useImperativeHandle+refis 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?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.
Angular 18: Deferred Views & Built-in Control Flow
The newest release of Angular – which is Angular 18 – is planned to be released on May 22nd, 2024. This is Google's super popular TypeScript based webapp framework that allows developers to create amazing web applications. The features, changes, and goodies of this release are deferrable views…