In this guide
A simple guide to this issue: useEffect Infinite Loop in React (The Dependency Array Trap). Learn why it happens, what to check first, and how to fix it.
A useEffect that fetches data, sets state, and then re-runs because of that state change is the classic infinite loop. The cure is understanding what actually changes between renders.
The Core Rule
React re-runs an effect whenever any value in its dependency array changes by reference. Primitives (string, number, boolean) compare by value; objects, arrays, and functions are recreated on every render, so they are *always* 'new'.
Trap 1: An Object or Array in Dependencies
// ❌ options is a new object every render → effect runs every render
const options = { page, size: 20 };
useEffect(() => { fetchUsers(options); }, [options]);
// ✅ Depend on the primitives
useEffect(() => { fetchUsers({ page, size: 20 }); }, [page]);Trap 2: A Function in Dependencies
If an effect depends on a function defined in the component, wrap that function in useCallback so its identity is stable:
const load = useCallback(async () => {
const res = await fetch(`/api/users?page=${page}`);
setUsers(await res.json());
}, [page]);
useEffect(() => { load(); }, [load]);Trap 3: Setting State the Effect Depends On
// ❌ effect depends on count and also changes it
useEffect(() => { setCount(count + 1); }, [count]);
// ✅ Use the functional updater and the right trigger
useEffect(() => { setCount((c) => c + 1); }, [someTrigger]);Don't 'Fix' It by Removing Dependencies
Deleting items from the dependency array to stop the loop hides stale-closure bugs. Fix the *identity* of the dependency with useCallback/useMemo instead.
Once you internalize 'objects and functions are new every render', useEffect loops become obvious — and the React DevTools 'why did this render' feature will point at the exact unstable dependency.
Relevant Scaleup Infotech Services
Keep Reading
Fix React Search Results Showing the Wrong Data
Fix React search race conditions with request cleanup, cancellation, and stale-response checks. Includes code and a slow-network verification checklist.
5 min readFix Next.js Environment Variables Missing in Production
Fix missing Next.js production environment variables by checking build-time values, public prefixes, deployment settings, and the exact release artifact.
5 min readFix Laravel Queue Jobs Not Processing After Deployment
Recover Laravel queue jobs after deployment by checking the connection, queue name, workers, cached configuration, failed jobs, and restart behavior.
5 min read