Skip to content

Latest commit

 

History

History
20 lines (10 loc) · 595 Bytes

File metadata and controls

20 lines (10 loc) · 595 Bytes

React may delay updates and group them together for performance. So if you do:

setCount(count + 1); setCount(count + 1);

Both lines see the same old count, and you finally get +1.

But if you do:

setCount(prev => prev + 1); setCount(prev => prev + 1);

Each update uses the latest value, so you finally get +2.

React gives you the function form to guarantee correctness when the next state depends on the previous one.

Think of state updates as:

“I’m describing how to compute the next state, not setting it directly.”

That’s the mental model React wants.