Performance & Optimization
useDebounceFn
About
A powerful debounce function hook that wraps any function with debounce functionality. This hook is useful for limiting the rate at which a function can fire, commonly used for search inputs, API calls, resize handlers, and other performance-sensitive operations.
Examples
Basic trailing debounce
Leading edge debounce
Search input with debounce
Advanced usage with maxWait
Arguments
| Argument | Type | Description | Default | 
|---|---|---|---|
| func | Function | The function to debounce | - | 
| delay | Number | The delay in milliseconds | - | 
| options | Object | Configuration options (see options table) | { leading: false, trailing: true } | 
Options
| Option | Type | Description | Default | 
|---|---|---|---|
| leading | Boolean | Execute the function on the leading edge of the delay | false | 
| trailing | Boolean | Execute the function on the trailing edge of the delay | true | 
| maxWait | Number | Maximum time the function can be delayed before it's forcibly executed | - | 
Returns
Returns an array with two elements:
| Return value | Type | Description | 
|---|---|---|
| debouncedFn | Function | The debounced version of the original function | 
| isDebouncing | Boolean | Whether the debounce timer is currently active | 
Notes
- At least one of leadingortrailingmust betrue. Setting both tofalsewill throw an error.
- If maxWaitis specified, it must be greater than or equal to thedelayvalue.
- The maxWaitoption ensures the function is called at least once everymaxWaitmilliseconds, even if the debounced function keeps being called.
- The debounced function preserves the original function's arguments and is type-safe.
- The isDebouncingboolean can be used to show loading states or disable UI elements while debouncing is active.