Skip to content

Commit

Permalink
docs: use-debounce-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaidev committed Nov 21, 2024
1 parent a0db1f8 commit 8936410
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/hooks/use-debounce-effect.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions src/use-debounce-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8936410

Please sign in to comment.