Skip to content

How to Fix useEffect Running Multiple Times

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to make useEffect run only once with correct dependency array. This matters because effects that run on every render cause performance issues. In real-world applications, this pattern appears in initial data loading analytics tracking subscription setup.

The Wrong Way

function Analytics() {
  useEffect(() => { trackPageView(window.location.pathname); }); // No deps
  return <div>Content</div>;
}
trackPageView fires on every render not just mount

The Right Way

function Analytics() {
  useEffect(() => { trackPageView(window.location.pathname); }, []);
  return <div>Content</div>;
}
trackPageView fires once on mount

Prevention

  • Use empty deps array for one-time effects
  • Include all reactive values the effect uses
  • Use exhaustive-deps ESLint plugin
  • Break complex effects into smaller focused effects

FAQ

### What does empty deps array do??

Tells React effect has no reactive dependencies. Runs only once after initial render. Empty array never changes so effect never re-runs.

How to handle StrictMode double-firing??

StrictMode intentionally double-invokes effects in dev. Expected behavior. In production effect runs once with empty deps.

Can I use ref to track first mount??

Yes const isFirst = useRef(true); useEffect(() => { if(isFirst.current){isFirst.current=false;return;} }, [deps]); but usually unnecessary.

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