Open History to see the last commit
1---
2id: useThrottledCallback
3title: useThrottledCallback
4---
5
6```ts
7function useThrottledCallback<TFn>(fn, options): (...args) => void;
8```
9
10Defined in: [throttler/useThrottledCallback.ts:43](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/throttler/useThrottledCallback.ts#L43)
11
12A React hook that creates a throttled version of a callback function.
13This hook is essentially a wrapper around the basic `throttle` function
14that is exported from `@tanstack/pacer`,
15but optimized for React with reactive options and a stable function reference.
16
17The throttled function will execute at most once within the specified wait time period,
18regardless of how many times it is called. If called multiple times during the wait period,
19only the first invocation will execute, and subsequent calls will be ignored until
20the wait period has elapsed.
21
22This hook provides a simpler API compared to `useThrottler`, making it ideal for basic
23throttling needs. However, it does not expose the underlying Throttler instance.
24
25For advanced usage requiring features like:
26- Manual cancellation
27- Access to execution counts
28- Custom useCallback dependencies
29
30Consider using the `useThrottler` hook instead.
31
32## Type Parameters
33
34### TFn
35
36`TFn` *extends* `AnyFunction`
37
38## Parameters
39
40### fn
41
42`TFn`
43
44### options
45
46[`ReactThrottlerOptions`](../interfaces/ReactThrottlerOptions.md)\<`TFn`, \{
47\}\>
48
49## Returns
50
51(...`args`) => `void`
52
53## Example
54
55```tsx
56// Throttle a window resize handler
57const handleResize = useThrottledCallback(() => {
58 updateLayoutMeasurements();
59}, {
60 wait: 100 // Execute at most once every 100ms
61});
62
63// Use in an event listener
64useEffect(() => {
65 window.addEventListener('resize', handleResize);
66 return () => window.removeEventListener('resize', handleResize);
67}, [handleResize]);
68```