# @react-hook/debounce

> A React hook for debouncing setState and other callbacks

Latest version **4.0.0** (published 2021-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @react-hook/debounce
pnpm add @react-hook/debounce
yarn add @react-hook/debounce
bun add @react-hook/debounce
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2021-06-18 |
| First published | 2019-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 36.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1530 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react hook, debounce, debounce hook, react debounce, debounce state, throttle react state, react debounce hook, use debounce, usedebounce |

## Links

- npm: https://www.npmjs.com/package/@react-hook/debounce
- Repository: https://github.com/jaredLunde/react-hook
- Homepage: https://github.com/jaredLunde/react-hook/tree/master/packages/debounce#readme
- Issues: https://github.com/jaredLunde/react-hook/issues
- npm.io page: https://npm.io/package/@react-hook/debounce

## Dependencies (1)

- [@react-hook/latest](https://npm.io/package/@react-hook/latest.md) ^1.0.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2021-06-18
- 3.0.0 — 2020-06-06
- 2.0.5 — 2020-05-25
- 2.0.4 — 2020-05-07
- 2.0.3 — 2020-04-24
- 2.0.2 — 2020-04-23
- 2.0.1 — 2020-04-23
- 2.0.0 — 2020-04-23
- 1.0.11 — 2020-01-07
- 1.0.9 — 2019-12-25
- 1.0.8 — 2019-11-26
- 1.0.7 — 2019-11-25
- 1.0.6 — 2019-11-24
- 1.0.5 — 2019-11-24
- 1.0.4 — 2019-08-23
- … 11 more at https://npm.io/package/@react-hook/debounce/versions

## README

<hr>
<div align="center">
  <h1 align="center">
    useDebounce()
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@react-hook/debounce">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@react-hook/debounce?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@react-hook/debounce">
    <img alt="Types" src="https://img.shields.io/npm/types/@react-hook/debounce?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.com/jaredLunde/react-hook">
    <img alt="Build status" src="https://img.shields.io/travis/com/jaredLunde/react-hook?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@react-hook/debounce">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@react-hook/debounce?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@react-hook/debounce?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @react-hook/debounce</pre>
<hr>

A React hook for debouncing setState and other callbacks

## Quick Start

```jsx harmony
import {useDebounce, useDebounceCallback} from '@react-hook/debounce'

const Component = (props) => {
  // at a basic level, used just like useState
  const [value, setValue] = useDebounce('initialValue')
}

const useMyCallback = (initialState, wait, leading) => {
  // this is the same code useDebounce() uses to debounce setState
  const [state, setState] = useState(initialState)
  return [state, useDebounceCallback(setState, wait, leading)]
}
```

## API

### useDebounce(initialState, wait?, leading?)

A hook that acts just like `React.useState`, but with a `setState` function
that is only invoked after the `wait` time in `ms` has been exceeded between
calls.

```ts
export const useDebounce = <State>(
  initialState: State | (() => State),
  wait?: number,
  leading?: boolean
): [State, Dispatch<SetStateAction<State>>]
```

#### Options

| Property     | Type                     | Default | Description                                                                                                                |
| ------------ | ------------------------ | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| initialState | `State \| (() => State)` |         | The initial state provided to `React.useState`                                                                             |
| wait         | `number`                 | `100`   | The amount of time in `ms` you want to wait after the latest call before setting a new state.                              |
| leading      | `boolean`                | `false` | Calls `setState` on the leading edge (right away). When `false`, `setState` will not be called until the next frame is due |

#### Returns `[state, setStateDebounced, setStateImmediate]`

| Variable          | Type       | Description                                |
| ----------------- | ---------- | ------------------------------------------ |
| state             | `State`    | The current value in state                 |
| setStateDebounced | `Function` | A debounced `setState` callback            |
| setStateImmediate | `Function` | The regular, immediate `setState` callback |

Note: Using `setStateImmediate` does not cancel a queued `setStateDebounced` calls,
i.e., the `setStateDebounced` will still take effect once its wait time is over.
If needed, cancelling behavior could typically still be achieved by calling both
`setStateImmediate` and `setStateDebounced` with the same value.

---

### useDebounceCallback(callback, wait?, leading?)

A hook that will invoke its callback only after `wait` time in `ms` has been
exceeded between calls.

```ts
export const useDebounceCallback = <CallbackArgs extends any[]>(
  callback: (...args: CallbackArgs) => void,
  wait = 100,
  leading = false
): ((...args: CallbackArgs) => void)
```

#### Options

| Property | Type                              | Default | Description                                                                                                                                                                          |
| -------- | --------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| callback | `(...args: CallbackArgs) => void` |         | This is the callback you want to debounce. You need to wrap closures/unstable callbacks in `useCallback()` so that they are stable, otherwise throttling will break between renders. |
| wait     | `number`                          | `100`   | Defines the amount of time you want `setState` to wait after the last received action before executing                                                                               |
| leading  | `boolean`                         | `false` | Calls `setState` on the leading edge (right away). When `false`, `setState` will not be called until the next frame is due                                                           |

#### Returns `debouncedCallback`

| Variable          | Type                              | Description                          |
| ----------------- | --------------------------------- | ------------------------------------ |
| debouncedCallback | `(...args: CallbackArgs) => void` | A debounced version of your callback |

## LICENSE

MIT

---
_Source: https://npm.io/package/@react-hook/debounce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
