Community JavaScript Snippet

useLatest Ref: The Anti-Stale-Closure Pattern

The five-line hook I reach for whenever an effect, a setTimeout, or an external subscription needs to call back into the latest value of a prop or state without re-binding.

useLatest Ref: The Anti-Stale-Closure Pattern

The five-line hook I reach for whenever an effect, a setTimeout, or an external subscription needs to call back into the latest value of a prop or state without re-binding.

JavaScript
Frontend
2 snippets
react
hooks
code-template
utility
petrawilson

By @petrawilson

December 24, 2025

·

Updated May 18, 2026

851 views

20

4.4 (8)

useLatest is the smallest custom hook I keep around: three lines of body, one assignment per render. The bug it solves is the most common cause of "why is my React app stale": a setTimeout or websocket handler is registered once, and the function it calls captured count from the render that registered it, not the current one. Running the assignment outside useEffect is intentional: effects fire after commit, but the render itself is what the next callback should see. The shim above does not re-render across calls, so accordion 1 fakes two renders to print the contrast: stale closure sees 0, useLatest sees the most recent value.