React Debounce Hooks (use-debounce)
use-debounce is a React library providing versatile hooks for debouncing values and callbacks, currently at version 10.1.1. The package maintains an active release cadence with frequent patch and minor updates. Its key differentiators include a minimal bundle size (under 1KB), compatibility with established debouncing implementations (like those in Lodash or Underscore), and robust server-rendering support. It offers the `useDebounce` hook for delaying updates to React state values and the `useDebouncedCallback` hook for throttling function executions, suitable for various interactive scenarios such as input field handling, API request optimization, and event listener management. Since version 10, server-side debouncing is opt-in via the `debounceOnServer` option, preventing unnecessary CPU utilization during SSR.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an ES Module context after `use-debounce` moved to module support in v9.0.0.fixUse ES module `import` syntax: `import { useDebounce } from 'use-debounce';` -
Module not found: Error: Can't resolve 'use-debounce/dist/index.js'
cause Your build configuration or explicit import path is still referencing the old CommonJS entry point (`dist/index.js`) after it changed to `dist/index.cjs` in v9.0.0.fixUpdate your import paths to `use-debounce` (preferred for modern bundlers) or `use-debounce/dist/index.cjs` if explicitly targeting CommonJS. -
TypeError: (0 , _useDebounce.useDebounce) is not a function
cause Incorrectly importing `useDebounce` as a default import when it is a named export, or misconfiguring a CommonJS import in a build environment expecting ESM.fixEnsure `useDebounce` is imported as a named export: `import { useDebounce } from 'use-debounce';`
Warnings
- breaking In v10.0.0, the ESM build output path was changed from `index.modern.js` to `index.mjs`. Projects directly referencing internal module paths in their build pipelines may require adjustment.
- breaking The v10.0.0 release introduced the `debounceOnServer` option, which defaults to `false`. This means debounced callbacks no longer execute on the server by default. If your application relies on server-side debouncing, you must explicitly set `debounceOnServer: true`.
- breaking Version 9.0.0 changed the CommonJS entry point path from `dist/index.js` to `dist/index.cjs`. This impacts projects using `require()` or build tools configured for the old path.
- gotcha Prior to v10.0.4, `useDebounce` and `useDebouncedCallback` exhibited incorrect behavior when both `leading` and `trailing` options were set to `true` specifically within React Strict Mode.
- gotcha Earlier versions (prior to v10.0.5) had a bug where the `isPending` state might not reset correctly if the tracked value for `useDebounce` had not changed, leading to potentially stale UI indications.
Install
-
npm install use-debounce -
yarn add use-debounce -
pnpm add use-debounce
Imports
- useDebounce
import useDebounce from 'use-debounce'; const useDebounce = require('use-debounce');import { useDebounce } from 'use-debounce'; - useDebouncedCallback
const debouncedCallback = require('use-debounce').useDebouncedCallback; import * as useDebounce from 'use-debounce';import { useDebouncedCallback } from 'use-debounce'; - DebounceOptions
import type { DebounceOptions } from 'use-debounce';
Quickstart
import React, { useState } from 'react';
import { useDebounce } from 'use-debounce';
export default function InputExample() {
const [text, setText] = useState('Hello');
// Debounce the 'text' value with a 1000ms delay
const [debouncedValue] = useDebounce(text, 1000);
return (
<div>
<h2>Debounced Input Example</h2>
<input
defaultValue={'Hello'}
onChange={(e) => {
setText(e.target.value);
}}
style={{ padding: '8px', fontSize: '16px' }}
/>
<p><b>Actual value:</b> {text}</p>
<p><b>Debounced value:</b> {debouncedValue}</p>
<p style={{ fontSize: '0.8em', color: '#666' }}>
The 'Debounced value' updates 1 second after you stop typing.
</p>
</div>
);
}