Skip to content

useThrottledCallback

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


✨ Overview

useThrottledCallback ensures the callback is only executed once within the specified delay period, even if called multiple times.
Useful for optimizing performance for events like window resizing, scrolling, or continuous user input.


📦 Import

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

🚀 Usage Example

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

const handleScroll = useThrottledCallback(() => {
  console.log("Scroll event triggered");
}, 300);

window.addEventListener("scroll", handleScroll);

🧩 API Reference

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

Parameters

ParameterTypeDescription
fn(...args: any[]) => voidThe callback function to throttle.
delaynumberThe minimum delay between calls (ms).

Returns

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

⚙️ Implementation

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

export function useThrottledCallback<T extends (...args: any[]) => void>(
    fn: T,
    delay: number
): (...args: Parameters<T>) => void {
    const lastCallRef = useRef(0);

    return useCallback(
        (...args: Parameters<T>) => {
            const now = Date.now();
            if (now - lastCallRef.current >= delay) {
                lastCallRef.current = now;
                fn(...args);
            }
        },
        [fn, delay]
    );
}

💡 Notes

  • Ensures callback is not called more than once per delay period.
  • Useful for performance optimization.

🧾 Type Definition

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

🧭 Summary

FeatureDescription
🧱 ThrottledLimits callback execution rate
⚡ LightweightNo dependencies
🧠 IntuitiveSimple API surface