Console Logs and Render Counting
What you'll learn: How to use console.log to see how many times your component renders—and why you'll see double logs in development.
The Debugging Technique
Placing a console.log statement directly in your component body reveals exactly how many times React calls your render function:
function MyComponent() {
console.log('MyComponent rendered');
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Every time you see that log, React executed your component function to produce new JSX.
The Strict Mode Surprise
In development, you'll see two console logs per render. Click the button once, see two logs. This is Strict Mode's double-render behavior catching impure components.
In production, you'll see one log per render—the real count.
Why This Matters
This simple technique helps you:
- Verify state updates trigger renders: Change state, expect a log
- Debug unexpected re-renders: Too many logs? Something's causing unnecessary renders
- Confirm optimizations work: After using
React.memo, logs should decrease
The Mental Model
Think of console.log in the component body as a "render counter." It's your eyes into React's render phase—showing you when React calls your function, not when it updates the DOM (remember: render ≠ DOM update).
Just remember to remove these logs before shipping to production—they're debugging tools, not features.
Key Takeaway: console.log in a component body counts renders—expect doubles in Strict Mode during development, singles in production.