Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardReactstale-closure pitfallsSingle-choice MCQ

A developer uses a ref to escape a stale closure inside a callback. Why does reading countRef.current inside the memoized callback give the latest value while count would be stale?

const [count, setCount] = useState(0); const countRef = useRef(count); countRef.current = count; const onTick = useCallback(() => { console.log(countRef.current); }, []);