Lifecycle & Effects
useDebouncedEffect
About
A version of useEffect that debounces the effect execution based on dependency changes. This hook is useful when you want to delay the execution of an effect until dependencies have stopped changing for a specified duration.
Common use cases include:
- Delaying API calls until the user stops typing in a search input
- Waiting for resize events to settle before performing expensive calculations
- Debouncing form validation
Examples
Basic usage with search input
With cleanup function
With leading option
With multiple dependencies
Arguments
| Argument | Type | Description | Default |
|---|---|---|---|
| effect | EffectCallback | The effect callback to run (can return a cleanup function) | Required |
| deps | DependencyList | Array of dependencies that trigger effect re-execution | Required |
| delay | number | The debounce delay in milliseconds | 500 |
| options | DebounceSettings | Optional debounce settings | undefined |
Options
The options parameter accepts the following settings:
| Option | Type | Description | Default |
|---|---|---|---|
| leading | boolean | Execute on the leading edge of the timeout | false |
| trailing | boolean | Execute on the trailing edge of the timeout | true |
| maxWait | number | Maximum time the effect can be delayed (milliseconds) | - |
Key Features
- Automatic Debouncing: Effect execution is delayed until dependencies stop changing
- Cleanup Support: Return a cleanup function from the effect, just like
useEffect - Configurable Timing: Customize delay and leading/trailing execution
- Cancellation on Unmount: Pending effects are cancelled when the component unmounts
- Dependency Tracking: Works just like
useEffectwith dependency arrays