"React useState async updates: What gets printed when you click the button?"

🧩 Output Challenge #11 — The Batching Confusion This one catches even experienced devs in real interviews 😅 function App() { const [count, setCount] = React.useState(0); const logCount = () => { console.log("Before:", count); setCount(count + 1); setTimeout(() => console.log("After:", count), 0); }; return ( <div> <p>{count}</p> <button onClick={logCount}>Increment</button> </div> ); } 🧠 Question: You click the “Increment” button once. What gets printed in the console? And why? (Hint: think about closures, batching, and React’s async state updates) 💬 Drop your answer + reasoning in the comments 👇 This one sparks massive discussions 🔥 #React #JavaScript #Frontend #Nextjs #TypeScript #Hooks #useState #CleanCode #Performance #DeveloperCommunity #InterviewPreparation #WebDevelopment

Log 0 , UI: 0 (because of stale closure)

To view or add a comment, sign in

Explore content categories