Skip to content

useDebouncedCallback

A React hook that returns a debounced version of a callback function.


✨ Overview

useDebouncedCallback delays invoking the callback until after a specified delay has elapsed since the last call.
Useful for search inputs, resize handlers, or any event that should not trigger immediately.


📦 Import

tsx
import { useDebouncedCallback } from 'react-hookstack';

🚀 Usage Example

tsx
import { useDebouncedCallback } from 'react-hookstack';

const handleChange = (value: string) => console.log(value);
const debouncedChange = useDebouncedCallback(handleChange, 500);

<input onChange={(e) => debouncedChange(e.target.value)} />

🧩 API Reference

useDebouncedCallback<T extends (...args: any[]) => void>(fn: T, delay: number): (...args: Parameters<T>) => void

Parameters

ParameterTypeDescription
fn(...args: any[]) => voidThe callback function to debounce.
delaynumberThe debounce delay in milliseconds.

Returns

TypeDescription
(...args: Parameters<T>) => voidDebounced version of the callback.

⚙️ Implementation

tsx
import { useRef, useCallback, useEffect } from "react";

export function useDebouncedCallback<T extends (...args: any[]) => void>(
    fn: T,
    delay: number
): (...args: Parameters<T>) => void {
    const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

    useEffect(() => {
        return () => {
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
            }
        };
    }, [delay]);

    return useCallback(
        (...args: Parameters<T>) => {
            if (timeoutRef.current) clearTimeout(timeoutRef.current);
            timeoutRef.current = setTimeout(() => fn(...args), delay);
        },
        [fn, delay]
    );
}

💡 Notes

  • Cleans up timeout on unmount.
  • Ensures only the last call within the delay is executed.

🧾 Type Definition

tsx
function useDebouncedCallback<T extends (...args: any[]) => void>(
  fn: T,
  delay: number
): (...args: Parameters<T>) => void;

🧭 Summary

FeatureDescription
🧱 DebouncedDelays callback execution
⚡ LightweightNo dependencies
🧠 IntuitiveSimple API surface