Skip to main content

useLatest

function useLatest<T>(value: T): RefObject<T>;

Returns a ref holding the latest value, for reading in effects, timers and event handlers without listing value as a dependency. The ref is the same object on every render. It is updated once a render commits, before any effects run (children's included), so read it there rather than while rendering.

Example​

import { useEffect } from "react";
import { useLatest } from "react-honey-hooks";

function Autosave({ draft }: { draft: string }) {
const latestDraft = useLatest(draft);

useEffect(() => {
const id = setInterval(() => {
save(latestDraft.current);
}, 10_000);
return () => {
clearInterval(id);
};
}, [latestDraft]);

return null;
}