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
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