Skip to content

How to Fix Throttled Callback in React

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to implement throttled callbacks correctly in React. This matters because throttled functions recreate every render losing state. In real-world applications, this pattern appears in scroll handlers resize handlers rate-limited API calls.

The Wrong Way

function ScrollTracker() {
  const [pos, setPos] = useState(0);
  const throttledHandler = throttle(() => setPos(window.scrollY), 200);
  useEffect(() => {
    window.addEventListener("scroll", throttledHandler);
    return () => window.removeEventListener("scroll", throttledHandler);
  }, [throttledHandler]);
  return <div>{pos}</div>;
}
throttledHandler re-created every render listener re-attaches

The Right Way

function ScrollTracker() {
  const [pos, setPos] = useState(0);
  const throttledRef = useRef(throttle(() => setPos(window.scrollY), 200));
  useEffect(() => {
    window.addEventListener("scroll", throttledRef.current);
    return () => window.removeEventListener("scroll", throttledRef.current);
  }, []);
  return <div>{pos}</div>;
}
Stable throttle instance listener added once removed on unmount

Prevention

  • Use refs for throttled functions to maintain stable instance
  • Create throttle outside component or use ref
  • Clean up throttled listeners in useEffect

FAQ

### Why does throttle not work in React??

Same as debounce -- functions recreate every render losing internal state.

What library for throttle??

lodash throttle reliable. rAF can serve as throttle for animation events.

Should I use throttle or debounce for scroll??

Throttle limits to once per interval. Debounce waits for pause. Throttle better for scroll which fires continuously.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro