Debugging with Phase Awareness
You'll learn how to use console.log placement and React DevTools to figure out when your code is running—during render, commit, or effects.
The Core Idea
When debugging React code, knowing which phase your code executes in is crucial. Is that console.log firing during the render phase (pure calculation), the commit phase (DOM updates), or the effects phase (after paint)? Placing logs strategically tells you what's happening and when.
Think of it like tracking a package: You wouldn't just see "your package moved"—you'd want timestamps showing when it left the warehouse, when it was loaded on the truck, and when it arrived. React's phases are similar checkpoints.
Strategic Console Log Placement
function MyComponent({ userId }) {
console.log('🎨 RENDER phase - pure calculation');
const [data, setData] = useState(null);
useLayoutEffect(() => {
console.log('📐 LAYOUT EFFECT - before paint, synchronous');
});
useEffect(() => {
console.log('⚡ EFFECT phase - after paint');
});
console.log('🎨 Still in RENDER phase');
return <div>User {userId}</div>;
}
When this component runs, you'll see:
🎨 RENDER phaselogs (both of them)📐 LAYOUT EFFECTlog- Browser paints
⚡ EFFECT phaselog
Using React DevTools Profiler
The Profiler tab shows you when each component rendered and how long each phase took. Enable "Record why each component rendered" to see what triggered the render (state change, parent re-render, etc.).
Key Takeaway: Place console.log statements in different locations (component body, useLayoutEffect, useEffect) to understand which phase code executes in. React DevTools Profiler gives you timing data for the render and commit phases, helping you spot performance bottlenecks.