0
Challenge · 26Hooks & Effects
DOM Manipulation
Coreused in ~75% of apps
Sometimes you need to do something React has no prop for — focus an input, scroll an element into view, measure its real size, or play a video. A ref hands you the actual DOM node so you can call those browser methods directly.
Core idea. Attach a ref to a JSX element and after React mounts it, ref.current is the real DOM node — call imperative methods on it (focus, select, scrollIntoView, getBoundingClientRect) for the things React doesn't expose. For everything else, let state drive the UI; don't fight React over the DOM.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
REF → REAL DOM NODE<input ref={inputRef} />
scrollable area — the input lives near the bottom ↓
…
○ not focused○ no selection
MEASURED SIZE
— × —
width × height (px)
Press a button — each one reaches the real DOM node through inputRef.current.
State never changed the text — these actions reach past React to the live node. That is exactly what refs are for: focus, selection, scroll, and measurement React gives you no prop for.
↑ play a little first, then dig in