Observing Batching with Console Logs
What you'll learn: How to use console.log statements in your component's render and effects to visually confirm that multiple state updates trigger only a single re-render.
Seeing Is Believing
You've learned that React batches multiple setState calls together—but how do you prove it? The simplest debugging technique is adding console.log statements in strategic places. When you log inside your component function (during render) and inside useEffect, you can count exactly how many times React re-renders your component.
The Logging Strategy
Place a console.log at the top of your component function:
function Counter() {
console.log('Counter rendered'); // Runs during render phase
const [count, setCount] = useState(0);
const [name, setName] = useState('');
function handleClick() {
setCount(count + 1);
setName('Updated');
console.log('Both updates called'); // Runs immediately
}
return <button onClick={handleClick}>Click me</button>;
}
What you'll see in the console:
"Both updates called"appears once (the event handler runs)"Counter rendered"appears once (not twice!)
Even though you called setCount and setName separately, React batched them into a single render. Without batching, you'd see "Counter rendered" twice.
Adding useEffect for Confirmation
You can also log inside useEffect to see when side effects run:
useEffect(() => {
console.log('Effect ran after render');
});
This confirms the render completed and the commit phase finished.
Key Takeaway: Console logs placed at the top of your component function count renders—use them to verify that multiple setState calls in React 18 produce only one render, proving automatic batching is working.