While trying to understand how React works, the concept of State was something that constantly gave me a headache. I didn't understand what it was ...
For further actions, you may consider blocking this person and/or reporting abuse
It can be dangerous updating the state like:
In these cases, it's better to make use of the callback function
setCount
provides:Hello, thanks a lot for the feedback! I'll edit the post accordingly. May i know why it could be dangerous to update the state that way?
In your example, you shouldn't run into any issues. However, as your app grows you may encounter that the state isn't updated reliably if you, for example, use the setter function inside a callback. This is a good example that demonstrates such a scenario. Click
Delayed Counter (basic)
and then immediately double clickImmediate Counter
. You will notice that that count will end up being1
, even though you clicked three times in total.There was recently a Dev post on this topic and here you can find the related documentation.
Wow! Thank you so much for the references. I'll check them out. I have updated the article as well.
Thank You
Thank you for reading, Sami!
Nice one my dearie
Thank you Ben
State in React has been a sore point for me too. Thank you for the post 👌🏽
Thank you so much for the feedback, Doaa.
Thank You
Thanks for reading!
Nice job breaking this down .
Thank you so much, Coreleon🙏