Join our Discord Community

useDebounceCallback

React hook for debouncing callback functions with advanced control options. Perfect for React applications requiring performance optimization with Next.js integration and TypeScript support.

Loading component...

Installation

npx shadcn@latest add https://www.shadcn.io/registry/use-debounce-callback.json
npx shadcn@latest add https://www.shadcn.io/registry/use-debounce-callback.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-debounce-callback.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-debounce-callback.json

Features

  • Lodash.debounce integration with full-featured debouncing using industry-standard library
  • Control functions including cancel, flush, and isPending methods for advanced control
  • Configurable options with leading, trailing, and maxWait parameters for fine-tuning
  • Automatic cleanup with useUnmount hook preventing memory leaks on component unmount
  • TypeScript support with complete type definitions and generic function preservation
  • Performance optimized using useMemo and useEffect for efficient re-renders

Examples

Advanced Search with Controls

Loading component...

Demonstrates debounced search with cancel, flush controls, and loading states.

Use Cases

This free open source React hook works well for:

  • Search inputs - Delay API calls until user stops typing built with Next.js
  • Form validation - Debounce validation checks using TypeScript patterns
  • Auto-save features - Delay save operations until user pauses editing
  • API throttling - Control request frequency to prevent rate limiting
  • Resize handlers - Debounce expensive calculations during window resize events
  • Button click protection - Prevent rapid successive clicks on action buttons

API Reference

useDebounceCallback

ParameterTypeDefaultDescription
funcT extends (...args: any) => ReturnType<T>requiredFunction to debounce
delaynumber500Delay in milliseconds
optionsDebounceOptionsundefinedAdditional lodash.debounce options

DebounceOptions

PropertyTypeDefaultDescription
leadingbooleanfalseExecute on the leading edge of the timeout
trailingbooleantrueExecute on the trailing edge of the timeout
maxWaitnumberundefinedMaximum time func is allowed to be delayed

Control Functions

FunctionTypeDescription
cancel() => voidCancel any pending function invocations
flush() => voidImmediately invoke any pending function invocations
isPending() => booleanCheck if there are any pending function invocations

Usage Pattern

const debouncedCallback = useDebounceCallback(
  (value: string) => console.log(value),
  300,
  { leading: true, trailing: false }
);

// Call the debounced function
debouncedCallback("search term");

// Control methods
debouncedCallback.cancel();    // Cancel pending calls
debouncedCallback.flush();     // Execute immediately
debouncedCallback.isPending(); // Check if pending

Implementation Notes

  • Hook uses lodash.debounce internally for robust, battle-tested debouncing behavior
  • Automatically cancels pending debounced functions when component unmounts
  • Preserves original function signature and return type through TypeScript generics
  • Uses useMemo to prevent unnecessary recreation of debounced function instances
  • Compatible with both synchronous and asynchronous callback functions
  • Requires lodash.debounce dependency for full functionality and control options