Updating React State
Why do we use React state and how is it updated?
The answer to these questions can be found in the previous post, in which we talked about the state of React.
Therefore, we recommend that before watching the video of this publication, you first see the video published previously in the aforementioned publication.
Updating state that depends on the previous state
About updating React state. We have to insist that when the React state is updated, the update does not always happen immediately. After executing the state update method, React schedules the state update. It is possible that there are multiple update operations and therefore the operation is not executed immediately.
In this video, we try to show how we should update the state if its new value depends on the value that previously exists in the same state. It is important that this update is done as we show in the video, otherwise, we can find wrong state values.
We are talking about situations such as handling a counter in which the new value it acquires always depends on the last value it contains before updating.