Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React hook: difference between setting the state with an argument or an arrow function

I would like to expand on the state update in the following issue

React Hooks, rerender & keeping same state - how it works underhood?

Is there a difference between the two ways of updating {count} ? Are they strictly equivalent ?

<button onClick={()=>setCount**(count+1)**} >{count}</button>

<button onClick={()=>setCount(**savedCount => savedCount+1)**}{count}</button>

Thanks in advance

like image 903
Makoto Avatar asked Mar 12 '26 20:03

Makoto


1 Answers

No they are not. You should prefer the latest, since it guarantees that the count your are working with is the latest version. When working directly with count rather than savedCount, it could have some updates still not applied.

like image 168
Nicolas SEPTIER Avatar answered Mar 17 '26 05:03

Nicolas SEPTIER



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!