Variable: useDebounce
constuseDebounce: <T>(callback,delay,options?) =>DebouncedFunction<T> =useDebouncedCallback
Defined in: use-debounce.ts:20
Debounces a function so it only runs after delay milliseconds have passed
since its last call.
Returns a stable, debounced version of callback that is only invoked once
delay milliseconds have passed since its last call, along with cancel,
flush and isPending controls.
The returned function keeps the same identity for the
lifetime of the component and always invokes the latest callback and
delay, so it is safe in dependency arrays and long-lived listeners. A new
delay applies from the next call; a pending timer is not rescheduled.
By default the callback runs on the trailing edge with the latest arguments.
With leading, the first call of a burst runs immediately; with both
leading and trailing, a trailing call only follows when there were
further calls during the burst. With both set to false the callback is
never invoked. The return value of callback is ignored. A pending call is
discarded on unmount, or flushed when flushOnUnmount is set. For
debouncing a value instead of a function call, see useDebouncedValue; for
rate-limiting a continuously changing value, see useThrottledValue.
Type Parameters
T
T extends (...args) => void
The type of the callback function to debounce.
Parameters
callback
T
The function to debounce.
delay
number
The debounce delay in milliseconds.
options?
Optional leading/trailing and unmount behavior.
Returns
A debounced function with cancel, flush and isPending.
Examples
const search = useDebouncedCallback((query: string) => {
fetchResults(query);
}, 300);
return <input onChange={(e) => search(e.target.value)} />;
// Autosave, flushing on blur and on unmount
const save = useDebouncedCallback(saveDraft, 1000, { flushOnUnmount: true });
return (
<textarea onChange={(e) => save(e.target.value)} onBlur={save.flush} />
);
// Ignore rapid repeat clicks, running only the first
const submit = useDebouncedCallback(sendForm, 1000, {
leading: true,
trailing: false,
});
For a live, editable example, see the useDebouncedCallback docs page.
Deprecated
Use useDebouncedCallback, which takes the same arguments and
returns the same stable function (plus cancel, flush and isPending),
or useDebouncedValue to debounce a value instead. This alias will be
removed in a future release.
Example
// Before
const debouncedSearch = useDebounce(search, 300);
// After
const debouncedSearch = useDebouncedCallback(search, 300);