useDebouncedAsyncEffect
About
A version of useEffect that accepts an async function and debounces its execution based on dependency changes. This hook combines the debouncing behavior of useDebouncedEffect with the async handling capabilities of useAsyncEffect.
This hook is particularly useful when you need to:
- Debounce API calls until the user stops typing
- Prevent race conditions in async operations
- Handle cleanup of async resources with debounced triggers
The hook provides a shouldContinueEffect callback that helps prevent race conditions by allowing you to check if the current effect is still valid before updating state.
Examples
Basic async data fetching with debounce
Preventing race conditions
With cleanup function
With leading option for immediate first execution
Error handling
Arguments
| Argument | Type | Description | Default |
|---|---|---|---|
| effect | AsyncEffect | Async function that receives shouldContinueEffect callback | Required |
| deps | DependencyList | Array of dependencies that trigger effect re-execution | Required |
| delay | number | The debounce delay in milliseconds | 500 |
| cleanup | CleanupFunction | Optional cleanup function that receives the previous result | undefined |
| options | DebounceSettings | Optional debounce settings | undefined |
Effect Function
The effect function receives a shouldContinueEffect callback that should be used to check if the effect is still valid before updating state or performing side effects. This prevents race conditions and memory leaks.
Effect Function Parameters
| Parameter | Type | Description |
|---|---|---|
| shouldContinueEffect | Function | Returns true if the effect is still valid and should continue |
Effect Function Return Value
The effect function can optionally return a value that will be passed to the cleanup function when the effect is cleaned up or re-run.
Cleanup Function
The cleanup function is called when:
- The component unmounts
- The dependencies change and the effect needs to re-run
Cleanup Function Parameters
| Parameter | Type | Description |
|---|---|---|
| result | T | The value returned by the previous effect function |
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
- Race Condition Prevention: The
shouldContinueEffectcallback prevents state updates from cancelled effects - Memory Leak Prevention: Automatic cleanup when components unmount or dependencies change
- Flexible Cleanup: Cleanup function receives the result from the previous effect execution
- Error Handling: Errors in async effects are properly propagated
- Configurable Timing: Customize delay and leading/trailing execution