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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With