Debugging Infinite Loops with Console Logs
You'll learn: A practical console logging technique to identify exactly which dependency in your effect is triggering infinite re-renders.
The Core Problem
When your useEffect causes an infinite loop, it's usually because a dependency changes on every render—but which dependency? If you have multiple dependencies or complex objects, the culprit isn't always obvious.
The Console Log Technique
Add a console.log statement inside your effect that logs each dependency individually:
useEffect(() => {
console.log('Effect ran!');
console.log('userId:', userId);
console.log('filters:', filters);
console.log('onSuccess:', onSuccess);
// ... rest of your effect
}, [userId, filters, onSuccess]);
Watch your console as the component renders. If you see the same log repeating endlessly, check which value is actually changing between logs. Often you'll discover:
- An object reference (
filters: { status: 'active' }) looks identical but is a new object each time - A function reference (
onSuccess: ƒ onSuccess()) recreates every render - A primitive looks stable but is actually being updated by the effect itself
Taking It Further
For object dependencies, log their JSON representation to see if the content changes:
console.log('filters JSON:', JSON.stringify(filters));
If the stringified version is identical across logs but the effect still loops, you've confirmed it's a reference problem (concepts 624, 630).
Key Takeaway: Console logging dependencies inside your effect is the fastest way to identify which value is unexpectedly changing and causing infinite re-renders—turning mysterious loops into obvious reference problems.