Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-debounced

dlavrenuek8.6kMIT1.1.3TypeScript support: included

Universal debounce hook for react

react, typescript, react-hooks, debounce

readme

Universal debounce hook for React

CI CodeQL npm version

react-debounced

Universal useDebounce hook which can be used for any debounced action. Only the last provided callback will be executed after a given timeout.

Installation

Install with yarn

yarn add react-debounced

Install with npm

npm install react-debounced

Example usage

Import the hook first

import useDebounce from 'react-debounced';

Use it in your functional components:

`typescript jsx const Test = () => { const debounce = useDebounce(); const [value, setValue] = useState(''); const [debounced, setDebounced] = useState('');

const handleInput = (e) => { const { value } = e.target; setValue(value); debounce(() => { // any functionality, like triggering api calls or setting a state, can be used here console.log('Debounced'); setDebounced(value); }); };

return ( <>

Value: {value}

Debounced: {debounced}

<input placeholder="Fill me out" value={value} onChange={handleInput} /> </> ); };


### Options

`useDebounce` has only one optional parameter `timeout`, which is set to 250ms per default.

#### Example with a 100 milliseconds timeout

```typescript jsx
const debounce = useDebounce(100);

Multiple debounce in one component

Each call of useDebounce inside a component will return a debounce function with its own timeout. If you need to debounce multiple input fields, just use:

typescript jsx const debounceOne = useDebounce(); const debounceTwo = useDebounce();

changelog

Changelog

1.1.3 (2025-01-10)

Features

  • add suport for React 19 (#442)

1.1.2 (2022-10-01)

Bug Fixes

1.1.1 (2022-09-29)

Bug Fixes

1.1.0 (2022-09-29)

Features

  • release react 18 support (d5dc229)

1.0.2 (2021-05-12)

Bug Fixes

  • bump hosted-git-info from 2.8.8 to 2.8.9 (#120) (e74d26e)