typescriptintermediate
useThrottle Hook for Rate Limiting
Throttle rapidly-firing values like scroll or resize events with a configurable delay hook.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useEffect, useRef, useState } from 'react';
function useThrottle<T>(value: T, delay: number): T {
const [throttled, setThrottled] = useState(value);
const lastExecuted = useRef(Date.now());
useEffect(() => {
const elapsed = Date.now() - lastExecuted.current;
if (elapsed >= delay) {
setThrottled(value);
lastExecuted.current = Date.now();
} else {
const timer = setTimeout(() => {
setThrottled(value);
lastExecuted.current = Date.now();
}, delay - elapsed);
return () => clearTimeout(timer);
}
}, [value, delay]);
return throttled;
}
// Usage Example
function ScrollTracker() {
const [scrollY, setScrollY] = useState(0);
const throttledY = useThrottle(scrollY, 200);
useEffect(() => {
const onScroll = () => setScrollY(window.scrollY);
window.addEventListener('scroll', onScroll, { passive: true });
return () => window.removeEventListener('scroll', onScroll);
}, []);
return (
<div className="fixed top-4 right-4 bg-black/80 text-white px-3 py-1 rounded">
<p>Raw: {scrollY}px</p>
<p>Throttled: {throttledY}px</p>
</div>
);
}
export { useThrottle, ScrollTracker };Use Cases
- Scroll position tracking
- Resize event handling
- Search-as-you-type rate limiting
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
React Intersection Observer Hook
Custom useIntersectionObserver hook for lazy loading, infinite scroll, and scroll animations.
Best for: Lazy loading components when they enter viewport
#react#hooks
typescriptintermediate
useThrottledValue Hook
Throttles a rapidly changing value to update at most once per specified interval.
Best for: Scroll position tracking
#hooks#throttle
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce
typescriptintermediate
Infinite Scroll with Intersection Observer
Load more items as the user scrolls using IntersectionObserver. No external libraries required.
Best for: Feed pagination
#infinite-scroll#intersection-observer