One of the most common surprises:

const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1);
console.log(count); // Still old value!
};

✅ Why?

React batches state updates for performance. The setCount call is asynchronous, so count doesn’t update immediately.

✅ Fix:

Use functional updates:

setCount(prev => prev + 1);

Leave a Reply

Your email address will not be published. Required fields are marked *