Skip to content
usefy

useThrottleCallback

Timing & async

A powerful React hook for throttled callbacks with cancel, flush, and pending methods

Install

$ npm install @usefy/use-throttle-callback

Quick start

useThrottleCallback.tsx
import { useThrottleCallback } from "@usefy/use-throttle-callback";

function ScrollTracker() {
  const handleScroll = useThrottleCallback(() => {
    console.log("Scroll position:", window.scrollY);
  }, 100);

  useEffect(() => {
    window.addEventListener("scroll", handleScroll);
    return () => {
      handleScroll.cancel();
      window.removeEventListener("scroll", handleScroll);
    };
  }, [handleScroll]);

  return <div>Scroll to see throttled logs</div>;
}

API reference

useThrottleCallback<T>(callback, delay?, options?)

A hook that returns a throttled version of the provided callback function.

Parameters

ParameterTypeDefaultDescription
callbackT extends (...args: any[]) => anyThe callback function to throttle
delaynumber500The throttle interval in milliseconds
optionsUseThrottleCallbackOptions{}Additional configuration options

Options

OptionTypeDefaultDescription
leadingbooleantrueInvoke on the leading edge (first call)
trailingbooleantrueInvoke on the trailing edge (after interval)

Returns ThrottledFunction<T>

PropertyTypeDescription
(...args)ReturnType<T>The throttled function (same signature as original)
cancel() => voidCancels any pending invocation
flush() => ReturnType<T> | undefinedImmediately invokes any pending invocation and returns its result
pending() => booleanReturns true if there's a pending invocation

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in timing & async