0
Challenge · 25Hooks & Effects

useRef

Coreused in ~85% of apps

Sometimes a component must remember a value across renders that the screen should NOT react to — a timer id, the previous value, a DOM node. Put it in state and you cause needless re-renders; put it in a plain variable and it resets every render. useRef is the box for exactly this.

Core idea. useRef returns a stable, mutable object { current } that survives every render. You read and write ref.current directly, and — unlike state — changing it does NOT trigger a re-render. It is the escape hatch for values you keep but don't display.
Playground, this isn't a screenshot
this component has rendered1times
useStatere-renders
const [count, setCount] = useState(0)
0

click → new value → screen updates

useRefno re-render
const ref = useRef(0)
0value from the last render

click → ref.current++ but screen frozen

Click + refa few times — nothing moves, because mutating a ref never re-renders. Now click + state once: the component re-renders, and the ref number jumps to reveal it had been counting the whole time.

↑ play a little first, then dig in