Skip to main content

useDebouncedCallback

function useDebouncedCallback<A extends unknown[]>(
fn: (...args: A) => void,
delayMs: number,
): DebouncedFunction<A>;

interface DebouncedFunction<A extends unknown[]> {
(...args: A): void;
readonly cancel: () => void; // drop the pending call
readonly flush: () => void; // run the pending call now
}

Returns a function that, however often it is called, runs fn once with the last arguments after delayMs without further calls. A delay of 0 or less (or NaN) still collapses calls made in the same task, and with Infinity the call waits for flush or cancel. The latest fn is always used, so inline functions are fine, and the returned function keeps its identity for the component's lifetime. A pending call keeps the delay it was scheduled with, and is dropped when the component unmounts.

Live demo​

All changes saved

Example​

import { useDebouncedCallback } from "react-honey-hooks";

function NoteEditor({ save }: { save: (text: string) => void }) {
const saveSoon = useDebouncedCallback(save, 1000);

return (
<textarea
onChange={(event) => {
saveSoon(event.target.value);
}}
onBlur={saveSoon.flush}
/>
);
}