What is "State" in React js?

What is "State" in React js?

React js State:

A React component can access data in two ways: props and state. Unlike props, a component’s state is not passed in from the outside. It is something that is internal to the component. In addition Every component has its own state in React.

State is something that can change over time. Therefore, changes in state affect the rendering of components. which means when the state Object changes, the component re-renders.

Setting initial state:

In order to do that we give the component a state property. This property should be declared inside of a constructor. As in the following example:

image.png

The state object can contain as many properties as you like For example,

image.png

Update state with this.setState:

A component can change its own state by calling the function this.setState().

image.png

The dIfference VS State snd Props:

1. Props:

-Props are passed to the component from the parent component.

-are immutable.

-Have a good performance.

-Use this to pass data to child components.

2. State:

-State is something internal and private to the component.

-State should store as simple data as possible.

-State can change.

-Every component has its own state in React.

State Recap:

these are the most important things that we have learned in React js "State":

1-The definition of State.

2-Setting Initial State.

3-Update State with this.setState.

4-The difference between state and props.

This is the end of the lesson I hope this blog may help you to improve yourself in React state If you have any questions I will be happy to read and answer your comments, good luck guys.