# @react-hook/throttle

> A React hook for throttling setState and other callbacks

Latest version **2.2.0** (published 2020-06-06) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2.2.0 |
| Published | 2020-06-06 |
| First published | 2019-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1530 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, react hook, throttle, throttle hook, react throttle, throttle state, throttle react state, react throttle hook, use throttle, usethrottle |

## Links

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

## 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

- 2.2.0 (latest) — 2020-06-06
- 2.1.4 — 2020-05-25
- 2.1.3 — 2020-05-07
- 2.1.2 — 2020-04-24
- 2.1.1 — 2020-04-24
- 2.1.0 — 2020-04-23
- 2.0.2 — 2020-04-23
- 2.0.1 — 2020-04-23
- 2.0.0 — 2020-04-23
- 1.0.12 — 2020-01-07
- 1.0.10 — 2019-12-25
- 1.0.9 — 2019-11-26
- 1.0.8 — 2019-11-25
- 1.0.7 — 2019-11-24
- 1.0.6 — 2019-11-24
- … 13 more at https://npm.io/package/@react-hook/throttle/versions

## README

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

<p align="center">
  <a href="https://bundlephobia.com/result?p=@react-hook/throttle">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@react-hook/throttle?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@react-hook/throttle">
    <img alt="Types" src="https://img.shields.io/npm/types/@react-hook/throttle?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@react-hook/throttle">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@react-hook/throttle?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/throttle?style=for-the-badge&labelColor=24292e">
  </a>
</p>

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

A React hook for performantly throttling `setState` and other callbacks.

## Quick Start

```jsx harmony
import {useThrottle, useThrottleCallback} from '@react-hook/throttle'

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

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

## API

### useThrottle(initialState, fps?, leading?)

A hook that acts just like `React.useState`, but with a `setState` function
that is only invoked at most X frames per second. A trailing call is guaranteed,
but you may opt-in to calling on the leading edge, as well.

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

#### Arguments

| Property     | Type                    | Default | Description                                                                                                                |
| ------------ | ----------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| initialState | `State | (() => State)` |         | The initial state provided to `React.useState()`                                                                           |
| fps          | `number`                | `30`    | Defines the rate in frames per second with which `setState` is invoked with 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, setState]`

| Variable | Type                              | Description                     |
| -------- | --------------------------------- | ------------------------------- |
| state    | `State`                           | The current value in state      |
| setState | `Dispatch<SetStateAction<State>>` | A throttled `setState` callback |

---

### useThrottleCallback(callback, fps?, leading?)

A hook that invokes its callback at most X frames per second. A trailing call is guaranteed,
but you may opt-in to calling on the leading edge, as well.

```ts
export const useThrottleCallback = <Callback extends (...args: any[]) => void>(
  callback: Callback,
  fps = 30,
  leading = false
): Callback
```

#### Arguments

| Property | Type                                | Default | Description                                                                                                                                                                          |
| -------- | ----------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| callback | `((...args: CallbackArgs) => void)` |         | This is the callback you want to throttle. You need to wrap closures/unstable callbacks in `useCallback()` so that they are stable, otherwise throttling will break between renders. |
| fps      | `number`                            | `30`    | Defines the rate in frames per second with which `setState` is invoked with 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 `throttledCallback`

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

## LICENSE

MIT

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