From 8936410d7c51afea9863fd93ce6b10ad01711a7e Mon Sep 17 00:00:00 2001 From: Yuwang Cai Date: Thu, 21 Nov 2024 20:39:41 +0800 Subject: [PATCH] docs: use-debounce-effect --- docs/hooks/use-debounce-effect.md | 53 +++++++++++++++++++++++++++++++ src/use-debounce-effect.ts | 23 ++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 docs/hooks/use-debounce-effect.md diff --git a/docs/hooks/use-debounce-effect.md b/docs/hooks/use-debounce-effect.md new file mode 100644 index 0000000..4e7789e --- /dev/null +++ b/docs/hooks/use-debounce-effect.md @@ -0,0 +1,53 @@ +# useDebounceEffect + +Debounce an effect. + +```ts +useDebounceEffect( + () => { + doSomething(); + }, + [], + { timeout: 1000 }, +); +``` + +## Parameters + +### effect + +- Type: `EffectCallback` + +The effect to be debounced. + +### deps + +- Type: `DependencyList` +- Default: `undefined` + +The dependencies of the effect, just like in `useEffect`. + +### options + +- Type: `UseDebounceEffectOptions` +- Default: `{}` + +The options to configure the hook. + +## Options + +### timeout + +- Type: `number` +- Default: 500 + +The time in milliseconds between the last call of the effect and the actual execution. + +Changes of this value will clear the current timer (with the old `timeout` value) and start a new one (with the new `timeout` value). + +### onMount + +- Type: `boolean` +- Default: `false` + +Whether to immediately call the effect and trigger the debouncing mechanism when the hook is mounted. diff --git a/src/use-debounce-effect.ts b/src/use-debounce-effect.ts index 947351f..5457dab 100644 --- a/src/use-debounce-effect.ts +++ b/src/use-debounce-effect.ts @@ -6,13 +6,36 @@ import { } from "react"; import { useLatest } from "./use-latest"; +/** + * The options to configure `useDebounceEffect` hook. + */ export type UseDebounceEffectOptions = { + /** + * The time in milliseconds between the last call of the effect and the + * actual execution. + * + * Changes of this value will clear the current timer (with the old `timeout` + * value) and start a new one (with the new `timeout` value). + * + * @default 500 + */ timeout?: number; + + /** + * Whether to immediately call the effect and trigger the debouncing + * mechanism when the hook is mounted. + * + * @default false + */ onMount?: boolean; }; /** * Debounce an effect. + * + * @param effect The effect to be debounced. + * @param deps The dependencies of the effect, just like in `useEffect`. + * @param options The options to configure the hook. */ export function useDebounceEffect( effect: EffectCallback,