Debugging Missing Dependency Issues
You'll learn how to use console.log inside effects to catch stale closure bugs before they bite you.
The Problem: What You Expect vs What You Get
When an effect isn't working as expected, the bug is often hiding in plain sight—your effect is using stale values because a dependency is missing. The symptom: you think your effect is reading the current prop or state, but it's actually using an old value from a previous render.
The Debugging Technique
The simplest way to catch this is to add console.log statements at the top of your effect to see exactly what values it's closing over:
useEffect(() => {
console.log('Effect running with:', { count, user, isActive });
// Your effect logic here
}, [count]); // Missing user and isActive!
When you run this, you'll immediately see that user and isActive never update in the logs—even when they change in your component. That's your smoking gun: the effect captured those values once and never got new ones.
The Analogy: Taking a Snapshot
Think of your effect like taking a photograph. Whatever values exist when the photo is taken are frozen forever in that picture. If you don't re-take the photo (re-run the effect) when things change, you're staring at an outdated snapshot.
Console logging shows you which snapshot your effect is looking at.
Quick Workflow
- Add logs for every external value your effect uses
- Trigger the bug (change props/state)
- Check the console: do the logged values update?
- If not, add them to the dependency array
Key Takeaway: Console logging the values your effect uses is the fastest way to spot missing dependencies—you'll instantly see which values are stuck in the past.