React Debounced Input Component

3.3.0 · active · verified Sun Apr 19

react-debounce-input is a React component that provides debounced `onChange` functionality for HTML input elements (like `<input>` or `<textarea>`) or any custom React component that follows the `value`/`onChange` prop pattern. It prevents excessive re-renders, state updates, or API calls during rapid user input by delaying the `onChange` callback until a specified `debounceTimeout` has elapsed since the last change. The current stable version is `3.3.0`, which added official React 18 support. The package maintains an active release cadence, addressing bug fixes, enhancing features (e.g., TypeScript support since v3.2.0, `inputRef` in v3.1.0), and keeping dependencies updated. Its primary differentiator is its straightforward, drop-in usability, enabling developers to easily integrate debouncing without writing custom logic, offering a simple alternative to more complex form libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `DebounceInput` with a functional React component, setting a minimum length and a debounce timeout. It also showcases state management for the debounced value.

import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import { DebounceInput } from 'react-debounce-input';

interface AppState {
  value: string;
}

const App: React.FC = () => {
  const [value, setValue] = useState<string>('');

  const handleChange = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
    console.log('Debounced onChange triggered:', event.target.value);
    setValue(event.target.value);
  };

  return (
    <div style={{ fontFamily: 'sans-serif', padding: '20px' }}>
      <h1>React Debounce Input Demo</h1>
      <label htmlFor="debounced-input" style={{ display: 'block', marginBottom: '10px' }}>
        Type here (min 2 chars, 300ms debounce):
      </label>
      <DebounceInput
        id="debounced-input"
        minLength={2}
        debounceTimeout={300}
        onChange={handleChange}
        placeholder="Start typing..."
        style={{ width: '300px', padding: '8px', fontSize: '16px', border: '1px solid #ccc', borderRadius: '4px' }}
      />

      <p style={{ marginTop: '20px', fontSize: '1.1em' }}>
        Current debounced value: <strong>{value}</strong>
      </p>
      <p style={{ color: '#666' }}>
        This input will only update the displayed value after 300ms of no typing and when at least 2 characters have been entered.
        Pressing Enter will also force a notification (by default).
      </p>
    </div>
  );
};

const appRoot = document.getElementById('app-root') || document.createElement('div');
appRoot.id = 'app-root';
document.body.appendChild(appRoot);
ReactDOM.render(<App />, appRoot);

view raw JSON →