A lightweight and reusable React hook for throttling function calls in React apps. It ensures a function is invoked at most once in a specified delay period — perfect for optimizing performance in scroll, resize, or rapid input events.
npm install @ehsaneha/react-throttleor
yarn add @ehsaneha/react-throttleimport React, { useState } from "react";
import { useThrottle } from "@ehsaneha/react-throttle";
const ScrollLogger = () => {
const throttledLog = useThrottle((val: string) => {
console.log("Throttled value:", val);
}, 1000); // Run once every 1000ms
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
throttledLog(e.currentTarget.scrollTop.toString());
};
return (
<div onScroll={handleScroll} style={{ overflowY: "scroll", height: 200 }}>
...
</div>
);
};function useThrottle<T extends (...args: any[]) => void>(
callback: T,
delay: number
): T;callback: The function you want to throttle.delay: The minimum time interval (in ms) between function calls.
- A throttled version of your function.
- Type-safe and framework-agnostic
- Debounce alternative for scroll/input/resize
- Cleans up timers on unmount
- Compatible with React 18 and 19
Tested with @testing-library/react and jest. Includes:
- Immediate call on first trigger
- Throttled execution for rapid triggers
- Proper cleanup on component unmount
Made with ❤️ by ehsaneha
This package is licensed under the MIT License. See LICENSE for more information.
Feel free to modify or contribute to this package!