Skip to content

Commit

Permalink
docs: use-debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaidev committed Nov 21, 2024
1 parent 8936410 commit 803bf2e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/hooks/use-debounce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# useDebounce

Debounce a value.

```ts
const debouncedValue = useDebounce(value, { timeout: 1000 });
```

## Parameters

### value

- Type: `T`

The value to be debounced.

### options

- Type: `UseDebounceOptions<T>`
- Default: `{}`

The options to configure the hook.

## Options

### timeout

- Type: `number`
- Default: 500

The timeout in milliseconds between the last action of change and the actual time of that change being applied.

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 start debouncing after the component is mounted.

## Returns

- Type: `T`

The debounced value.
23 changes: 23 additions & 0 deletions src/use-debounce.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import { useState } from "react";
import { useDebounceEffect } from "./use-debounce-effect";

/**
* The options to configure the `useDebounce` hook.
*/
export type UseDebounceOptions = {
/**
* The timeout in milliseconds between the last action of change and the
* actual time of that change being applied.
*
* 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 start debouncing after the component is mounted.
*
* @default false
*/
onMount?: boolean;
};

/**
* Debounce a value.
*
* @param value The value to be debounced.
* @param options The options to configure the hook.
*
* @returns The debounced value.
*/
export function useDebounce<T>(value: T, options: UseDebounceOptions = {}) {
const [debouncedValue, setDebouncedValue] = useState(value);
Expand Down

0 comments on commit 803bf2e

Please sign in to comment.