Sub topic 2
A value that survives renders without causing them
This chapter assumes you know useState, how a function component re-runs on each render, and what a closure is. It explains why a ref keeps its value between renders and why changing it does not trigger one.
One object for the component's whole life
A component function runs again on every render, so ordinary local variables are recreated each time. State survives because React stores it outside your function. A ref survives for the same reason.
On the first render, useRef(initialValue) creates an object shaped like { current: initialValue }. React stores it with the component instance. On every later render, useRef returns that same object, and the argument is ignored.
Two facts follow from this:
- The object keeps the same identity across renders, so whatever you put in
currentis still there next time. - React does not watch
current. Assigning to it is a plain JavaScript property write, so no render is scheduled.
Seeing it work
The component below has one state value and one ref. The ref button changes clicksRef.current but the screen does not update. The read button then shows that the value was kept.
import { useRef, useState } from 'react';
export function ClickCounter() {
const [shown, setShown] = useState(0);
const clicksRef = useRef(0);
function countSilently() {
clicksRef.current += 1;
console.log('ref value:', clicksRef.current);
}
return (
<div>
<p>State: {shown}</p>
<button onClick={countSilently}>Increment ref</button>
<button onClick={() => setShown(shown + 1)}>Increment state</button>
<button onClick={() => alert(`Ref holds ${clicksRef.current}`)}>
Read ref
</button>
</div>
);
}Click "Increment ref" three times. The console logs 1, 2, 3, and the paragraph still shows State: 0. Click "Increment state" and the component re-renders. The ref still holds 3, because the re-render reused the same object.
What "survives" means
A ref keeps its value across re-renders of the same mounted component. It does not outlive the component. If the component unmounts, or its parent gives it a different key or removes it from the tree, React discards the ref and the next mount starts again from the initial value. Two instances of the same component each get their own ref, so they never share a current.
Why a plain variable does not work
A beginner's first alternative is a variable declared in the component body:
let clicks = 0; // reset to 0 on every renderThis is recreated on each render, so it loses its value. A variable declared outside the component survives, but then every instance of the component shares it, and it also persists after unmount. A ref gives you per-instance storage that is tied to the component's lifetime.
Where to read and write
Change ref.current in event handlers and effects, not while rendering. Because React ignores the write, the screen can show stale data if you print ref.current in JSX. A later chapter covers render purity. For now, remember that the ref stays up to date while the screen only updates when something else triggers a render.
Exercise
A component stores a counter in useRef(0) and displays {countRef.current} in its JSX. A button handler runs countRef.current += 1. The handler runs five times and the screen still shows 0. Why, and what happens to the shown number if an unrelated state update later causes a render?
Show answer
Writing to current does not schedule a render, so React never re-runs the component after the clicks and the screen keeps its old output. The ref itself does hold 5. When the unrelated state update triggers a render, the component reads the same ref object, so the screen then shows 5. If the number must appear on screen as it changes, it belongs in state.