React Debounce Hooks (use-debounce)

10.1.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates how to debounce a React state value using the `useDebounce` hook, updating a displayed value only after a specified delay.

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>
  );
}

view raw JSON →