Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

useDebouncedCallback.md

67 lines1.6 KB

1---
2id: useDebouncedCallback
3title: useDebouncedCallback
4---
5​
6```ts
7function useDebouncedCallback<TFn>(fn, options): (...args) => void;
8```
9​
10Defined in: [debouncer/useDebouncedCallback.ts:42](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/debouncer/useDebouncedCallback.ts#L42)
11​
12A React hook that creates a debounced version of a callback function.
13This hook is essentially a wrapper around the basic `debounce` function
14that is exported from `@tanstack/pacer`,
15but optimized for React with reactive options and a stable function reference.
16​
17The debounced function will only execute after the specified wait time has elapsed
18since its last invocation. If called again before the wait time expires, the timer
19resets and starts waiting again.
20​
21This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic
22debouncing needs. However, it does not expose the underlying Debouncer instance.
23​
24For advanced usage requiring features like:
25- Manual cancellation
26- Access to execution counts
27- Custom useCallback dependencies
28​
29Consider using the `useDebouncer` hook instead.
30​
31## Type Parameters
32​
33### TFn
34​
35`TFn` *extends* `AnyFunction`
36​
37## Parameters
38​
39### fn
40​
41`TFn`
42​
43### options
44​
45[`ReactDebouncerOptions`](../interfaces/ReactDebouncerOptions.md)\<`TFn`, \{
46\}\>
47​
48## Returns
49​
50(...`args`) => `void`
51​
52## Example
53​
54```tsx
55// Debounce a search handler
56const handleSearch = useDebouncedCallback((query: string) => {
57 fetchSearchResults(query);
58}, {
59 wait: 500 // Wait 500ms between executions
60});
61​
62// Use in an input
63<input
64 type="search"
65 onChange={(e) => handleSearch(e.target.value)}
66/>
67```