Skip to content

useThrottle

A React hook for throttling a changing value, ensuring updates occur at most once per specified interval.


✨ Overview

useThrottle limits how often a value can update, making it ideal for performance optimization in scroll, resize, or rapid input scenarios.


📦 Import

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

🚀 Usage Example

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

function ScrollTracker() {
  const [scrollY, setScrollY] = useState(window.scrollY);
  const throttledScrollY = useThrottle(scrollY, 200);

  useEffect(() => {
    const onScroll = () => setScrollY(window.scrollY);
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  useEffect(() => {
    console.log("Throttled scroll position:", throttledScrollY);
  }, [throttledScrollY]);

  return <div>Scroll Y: {throttledScrollY}</div>;
}

🧩 API Reference

useThrottle<T>(value: T, delay?: number): T

Parameters

ParameterTypeDefaultDescription
valueTThe value to throttle.
delaynumber300Minimum interval in milliseconds.

Returns

PropertyTypeDescription
(return value)TThe throttled value.

⚙️ Implementation

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

export function useThrottle<T>(value: T, delay: number = 300): T {
    const [throttledValue, setThrottledValue] = useState<T>(value);
    const lastExecuted = useRef<number>(Date.now());

    useEffect(() => {
        const now = Date.now();
        const remaining = delay - (now - lastExecuted.current);

        if (remaining <= 0) {
            setThrottledValue(value);
            lastExecuted.current = now;
        } else {
            const timeout = setTimeout(() => {
                setThrottledValue(value);
                lastExecuted.current = Date.now();
            }, remaining);

            return () => clearTimeout(timeout);
        }
    }, [value, delay]);

    return throttledValue;
}

export default useThrottle;

💡 Notes

  • Ensures the value updates at most once per delay interval.
  • Useful for scroll, resize, or rapid input events.
  • Works with any data type.

🧾 Type Definition

tsx
function useThrottle<T>(value: T, delay?: number): T;

🧭 Summary

FeatureDescription
⏱ Throttled valueUpdates at most once per interval
🧩 Generic supportWorks with any data type
⚡ LightweightNo dependencies
🧠 IntuitiveSimple API surface