state in react

State is an object in JavaScript that holds dynamic component data. And enables tracking between renderings. Because it is dynamic, it only makes interaction possible. For this reason, it is not used in static projects.

Components are classes that have additional properties, and local state means a property that classes have. Properties can only be used in classrooms. And usually the only place that can be declared as a manufacturer is this state.

State management takes place within the component. State in the react component is like a variable inside a function. The states in the react component are their own local state, ie they are accessed or changed outside the component and only inside the component. This is like a function that has its own local scope.

What is the role of setstate ()?

In fact, setState () is a scheduling task for updating a component object. When a state changes, the component responds again through rendering.

How to use States correctly

  • Do not change State directly.
  • Updates in States are out of date.
  • State updates merged.

We will now examine each to better understand its meaning.

Do not change State directly.

We say we should not change it directly, because it prevents the component from being re-rendered automatically. The only place this.state can be assigned is in the constructor.

Untimely state updates

React can combine multiple calls to function together in one update. That’s why they call setstate. Calling setstate is asynchronous when inside event handlers. We are not currently using this.state to display a new value immediately after calling setstate. To correct this situation we must use another form of the setstate function. Which accepts a function instead of an object. Make sure the latest state version is called.

Merge mode update

When we call setstate, we are actually merging an object that has been sent to the current state. For example, we can define several separate variables for each state. For this reason, states can be updated separately with different setSatet () calls.

Downward circulation of data

 Downward circulation of data

The parent components and the child components can never know if a component is stateless or stateful. And it does not matter if the component is defined as a function or a class. In general, it can be said that the information is only available to the owner component. This is why state is often called local or encapsulated. And information is only accessible from the top down.

state in react
        

Leave a Reply

Your email address will not be published. Required fields are marked *