Why do we use React state?
State allows us to manage to change data in an application. It’s defined as an object where we define key-value pairs specifying various data we want to track in the application. In React, all the code we write is defined inside a component.
State is updated asynchronously
State is updated asynchronously and the state is constant within a particular render of a react component.
In this video, we try to show how State does not update values immediately. When you change the state of React through the update function, the update operation is scheduled, which will be carried out when React decides, it is usually an immediate process but it does not always have to be that way. When the value of the state is changed, sometimes we have to take into account that this operation is not immediate and when changing the value we have to use a function that takes the old value of the state as a parameter, but that is something we are not seeing here. We will see this in a future post.
State is not an exclusive concept of React, although it is a key concept of this library. To establish the values of the State, the useState function is used, which is one of the most important hooks in the library.
When the useState function is called, it is React that is in charge of establishing the values of the State and that is why we declare the elements returned by this function as constants. We don’t directly manipulate the State variables and so it is correct to declare as a constant the special State variable that is handled by React in the background.