Mixed state updates in one click
Mount App, then click once. What logs during the handler, and what is the final button text?
Code Snippet
import { useState } from "react";
export default function App() {
const [n, setN] = useState(2);
return <button onClick={() => {
setN(n + 4);
setN(x => x * 3);
setN(n - 1);
setN(x => x + 8);
console.log(n);
}}>{n}</button>;
}Expected Output & Explanation
Console: 2
Button: 9 The handler’s n stays 2. React applies the queue as replace with 6, multiply to 18, replace with 1, then add 8.
Module Context & References
React · Predict the behavior Trace state queues, stale snapshots, keys, effects and refs
Personal Notes · Markdown