Capítulo 62 de 116
useRef(initialValue) is the API reference for the mutable-box pattern from Ch 34: returns { current: initialValue }, a stable object across renders whose .current can be freely read/written without causing or being subject to re-renders.
const ref = useRef(initialValue) — initialValue is only used on the very first render; the same ref object identity persists for the component's whole lifetime.ref attribute (Ch 35).ref.current doesn't re-render — this is the defining, deliberate difference from useState; if a UI update needs to reflect the change, a ref is the wrong tool and state is the right one..current during rendering — same rule as Ch 34: confine ref access to event handlers and Effects to keep the render function pure (Ch 18); the one exception is lazily initializing a ref's value on first render (checking if (ref.current === null) before setting it once), which is a recognized, safe pattern.ref.current is null until the element mounts, and is set back to null on unmount.function Stopwatch() {
const intervalRef = useRef(null);
function handleStart() {
intervalRef.current = setInterval(tick, 1000);
}
function handleStop() {
clearInterval(intervalRef.current);
}
}
useState; if not, use useRef..current is only guaranteed populated after commit — never assume it's set during the render body.ref.current === null inside the render body) is the sole sanctioned exception to "never read/write refs during render."