Skip to main content

Function: useLatest()

useLatest<T>(value): object

Defined in: use-latest.ts:30

Returns a ref that always holds the latest value passed to the hook.

Useful for reading up-to-date props, state or callbacks from long-lived closures (timers, event listeners, async callbacks, effects with empty dependency arrays) without re-subscribing or capturing stale values.

The ref is synchronized in a layout effect (a regular effect during server rendering), so it is updated after each commit but before any useEffect runs, including those of child components. Read ref.current inside callbacks or effects, not while rendering.

Type Parameters​

T​

T

Parameters​

value​

T

The value to keep in the ref.

Returns​

object

A stable ref object whose current is the latest value.

current​

readonly current: T

Example​

const onTickRef = useLatest(onTick);

useEffect(() => {
const id = setInterval(() => onTickRef.current(), 1000);
return () => clearInterval(id);
}, [onTickRef]);