useDebouncedAsync
Runs run(value, { signal }) once value has stopped changing for delayMs, and returns the latest result. The previous result stays while the next one is pending. A new value, disabling or unmounting aborts the signal and drops any late result.
Use for
- a debounced, abortable async check as the user types
- a password strength or availability check on an input
Practices
| Guidance | Practice |
|---|---|
| Do | Pass a primitive or memoized value. It is compared with Object.is, so an object built during render restarts the debounce every render. |
| Do | Forward signal to the request so a superseded check is cancelled. |
| Don’t | Debounce with setTimeout in a feature. Use this hook instead. |
Options
| Prop | Type | Description |
|---|---|---|
delayMs | number | Wait this long after the last change before running. |
enabled | boolean | When false, clears the result and cancels any waiting or running check.Default: true |
Returns
| Prop | Type | Description |
|---|---|---|
data | TData | The result of the last settled run for the current or a previous value. |
error | unknown | What the last settled run rejected with. |
isError | boolean | Whether the last settled run rejected. |
isPending | boolean | Whether no run has settled yet for the current value. Always false while disabled. |
Examples
A password strength check
const strength = useDebouncedAsync(password, (value, { signal }) => checkStrength(value, { signal }), {
delayMs: 300,
enabled: password !== '',
});