Store non-rendering data in refs
Keep information that does not drive the screen
A ref is a small mutable container owned by a component instance. Import useRef and call it at the top level. Its current property starts with the supplied value and survives subsequent renders of that instance. Unlike a state setter, assigning current does not request another render. Use refs for handles and bookkeeping, and state for information that must appear in JSX. Each positive example is a standalone App.jsx for the reviewed Chapter 3 client project; visual outcomes are expected, not browser-tested.
import { useRef, useState } from 'react';export default function App() { const sequence = useRef(0); const [last, setLast] = useState('No sample recorded'); return <main><h1>Sample station</h1> <button onClick={() => { sequence.current += 1; }}>Reserve number</button> <button onClick={() => setLast('Reserved through ' + sequence.current)}>Show latest number</button> <p>{last}</p> </main>;}Reserve number twice, then Show latest number: the expected text is Reserved through 2. Reserving alone leaves the displayed message unchanged. The ref stores the sequence; state records a snapshot for display. Two mounted copies have independent counters, and remounting one starts its counter again.
Use a ref for a cancellation handle
import { useEffect, useRef, useState } from 'react';function cancelPending(handle, cancel) { if (handle.current !== null) { cancel(handle.current); handle.current = null; }}export default function App() { const pending = useRef(null); const [message, setMessage] = useState('Idle'); useEffect(() => () => cancelPending(pending, clearTimeout), []); function schedule() { cancelPending(pending, clearTimeout); setMessage('Pending'); pending.current = setTimeout(() => { pending.current = null; setMessage('Reminder ready'); }, 1000); } function cancel() { cancelPending(pending, clearTimeout); setMessage('Cancelled'); } return <main><h1>Sample reminder</h1> <button onClick={schedule}>Schedule</button><button onClick={cancel}>Cancel</button> <p role="status">{message}</p></main>;}Schedule replaces an outstanding timeout; Cancel clears it if present. The effect cleanup also cancels it when the component unmounts. The handle is not part of the screen; the message is. A real timeout may execute later than requested. The helper uses null as its sentinel rather than testing truthiness, so even a handle of zero can be cleared.
Do not render directly from a mutable ref
import { useRef } from 'react';export default function App() { const count = useRef(0); return <main><p>Samples: {count.current}</p> <button onClick={() => { count.current += 1; }}>Add sample</button></main>;}Incorrect source for review: clicking updates the ref but does not schedule a render. If unrelated work later causes a render, the value may appear to jump. Replace the ref with useState and update through its setter. Avoid accessing current in the render body; one narrow exception is deterministic, side-effect-free initialization guarded by current === null, when the result is predictable and only initialized once per retained instance. Do not initialize timers or register resources that way. Development StrictMode can discard an extra render and its ref object.
Practice
Predict the screen after reserving three numbers, showing the number, and reserving one more. Explain which value should be state if the display must update after every reserve.