Skip to content

useWatchDebounced

Category
Export Size
632 B
Last Changed
5 days ago

Debounced watch. The callback will only be invoked after the source stops changing for the specified duration

Demo

Usage

Similar to useWatch, but offering extra options debounce and maxWait which will be applied to the callback function.

tsx
import { 
useWatchDebounced
} from '@reause/shared'
useWatchDebounced
(
input, () => {
console
.
log
('changed!') },
{
debounce
: 500,
maxWait
: 1000 },
)

Options

OptionTypeDefaultDescription
debouncenumber0Debounce delay in ms
maxWaitnumberMaximum wait time before forced invocation
immediatebooleanfalseFire the callback once on mount with the current value (still debounced)

Fire the callback once on mount with the current value (still debounced):

tsx
import { 
useWatchDebounced
} from '@reause/shared'
useWatchDebounced
(input, () =>
console
.
log
('changed!'), {
immediate
: true })

Type Declarations

ts
export interface UseWatchDebouncedOptions extends DebounceFilterOptions {
    debounce?: number;
    immediate?: boolean;
}

export interface DebounceFilterOptions {
    maxWait?: number;
    rejectOnCancel?: boolean;
}

export interface UseWatchCallback<T = any> {
    (value: T, oldValue: T | undefined): void;
}

export function useWatchDebounced<T extends any[]>(source: readonly [
    ...T
], callback: UseWatchCallback<[
    ...T
]>, options?: UseWatchDebouncedOptions): void

export function useWatchDebounced<T>(source: T, callback: UseWatchCallback<T>, options?: UseWatchDebouncedOptions): void

export function useDebounceFn<T extends FunctionArgs>(fn: T, ms?: number, options?: DebounceFilterOptions): UseDebounceFnReturn<T>

export function useWatch<T extends any[]>(source: readonly [
    ...T
], callback: UseWatchCallback<[
    ...T
]>, options?: UseWatchOptions): void

export function useWatch<T>(source: T, callback: UseWatchCallback<T>, options?: UseWatchOptions): void

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
913ce - fix(shared): resolve useWatchDebounced audit findings (#764)
8fddc - chore!: remove all Vue-only Maybe* types and getter unions, adopt React Ref (#462)

Released under the MIT License. v0.1.8