Introduction
In the previous article, we learned about JSX, hooks, and props. In this article, we are going to see what a State is, what are its uses, and how a State is different from props in React.
State in React
A State in React can be changed as per the user's action or any other actions. When the state is changed, React re-renders the component automatically to the browser. We can say that the State contains the information that can be changed during the component’s lifecycle. React stores the state of a component in this state. The value of the state can be set in 2 ways depending on the way a component is created.
Below are the two ways by which the value for a state can be set or initialized.
// Using React.createClass
var Counter = React.createClass({
getInitialState: function(){
return {counter: 0};
}
});
or
// Using ES6 classes
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
}
}
The component state can be changed by calling the following code.
this.setState(data, callback);
In this.setState() method, it performs a shallow merge of data, which then re-renders the component. The data argument under the setState() method contains an object or function that contains keys to be updated. The optional callback parameter under the setState method contains a function.
setState() is asynchronous, it does not immediately update values if the user tries to access this. state immediately after setState(), there are chances to get old values. This method schedules an update, computation is delayed until necessary.
In State, 3 things need to be taken care of
- The state should not be assigned a value directly other than in the constructor, always use the setState() method to assign value.
- State updates may be asynchronous, so the user should not rely on state values for calculating the next state value.
- State updates are shallow merges, In setState() multiple variables may be present so it merges values even though accessed or updated separately.
The state is also stated as local or encapsulated, as it can only be accessed in a function or class that owns it, irrespective of whether it is in stateless or stateful components.
Reconciliation process
React has a process named Reconciliation, this process includes how React updates the DOM after making changes to a component when there is a change in state. When the setState() method is triggered, React creates a new tree containing the React element in the component along with the updated State. This tree is used to find out how a component’s UI should change in response to state change by comparing its element with the previous tree. As React knows which changes need to be done that’s why React will only update that part of DOM making React perform fast.
Demo
In React, here is a demo that uses State. In this demo, we create a component named StateDemo.js under the component folder.
Now, let us add the code as below.
import React from 'react';
class States extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.onClick = this.onClick.bind(this);
}
render() {
return (
<p onClick={this.onClick}>{this.state.count}</p>
);
}
onClick() {
this.setState({ count: this.state.count + 1 });
console.log(this.state.count);
}
}
export default States;
Now, in app.js, import your newly created component and define its tag in the App class.
import React, { Component } from 'react';
import './App.css';
import logo from './logo.svg';
import States from './Components/StateDemo';
class App extends Component {
render() {
return (
<p className="App">
<img src={logo} className="App-logo" alt="logo" />
<States />
</p>
);
}
}
export default App;
Now, run your application. This will display your output in the browser.

Now, on clicking on p where 0 is displayed, it will increase the counter based on clicks.



Rushi MehtaPosted Aug 30, 2019, 11:10 PM
Great articles. Learning a lot from your article
Priyanka JainPosted Jul 7, 2019, 11:12 AM
Thank you all for your comments
Aakash MauryaPosted Jul 4, 2019, 10:56 AM
Great One.. Expecting a series..
Former memberPosted Jul 3, 2019, 4:07 AM
Nice article...
Mahesh VermaPosted Jul 2, 2019, 10:35 PM
Nice Explanation..thanks for sharing.