States are instances of a class component (note that functional components do not offer states) that can be defined as having many individual information. All the information mentioned in a particular state, can be utilized to render different things on screen.
States can be changed for the same component and the output can be updated as a response to user action or system event.
States can be defined in following two ways:
Inside the class (Without Constructor): State is written as the very first module of a component. Consider the code below:
class Intro extends Component {
state = {
name: "Shashi",
designation: "Faculty"
}
render(){
return(
Hi! {this.state.name}
You are a {this.state.designation}
);
}
}
export default Intro;
Here in this code:
Now the properties defined in the state will be used by the render() function of same component.
The syntax to use properties of a state is:
Hi! {this.state.name}
You can simply understand that the output will be: Hi! Shashi
Here, Hi!
Is written in the h2
and Shashi
comes from this (meaning the same component).state (meaning the state).name (meaning the property)
Also keep in mind that states cannot be used by any other component.
Inside the constructor of the class (With Constructor): As we already know, constructor is a method that creates new objects for any class. Somewhat similar to that, we have a method constructor() in React that has to be defined to hold the state in it. Another method super(); has to be called as the very first statement in the constructor. Consider the code below:
class Intro extends Component {
constructor () {
super ();
this.state = {
name: "Shashi",
designation: "Faculty"
}
}
render(){
return(
Hi! {this.state.name}
You are a {this.state.designation}
);
}
}
export default Intro;
Here in this code:
The output of this code will again be the same as of previous:
Hi! Shashi
You are a Faculty
NOTE: The reason behind calling super(); method first, is the theory that every child class constructor must first call the parent class (or super class) constructor. Here, since Intro is the child class having component as super class; it must first call the constructor of component class.
As we have discussed earlier, props are external entities that can be passed as argument within components. Props can be passed not only to a component but to a state of a component as well.
In order to use props under state, the props must be passed first to the constructor then to the super(); method. Then only we can assign the respective prop to a state property. Consider the code:
class Intro extends Component {
constructor (props) {
super (props);
this.state = {
name: "Shashi",
designation: "Faculty",
qlf: this.props.attr
}
}
render(){
return(
Hi! {this.state.name}
You are a {this.state.designation}
You have qualified {this.state.qlf}
);
}
}
export default Intro;
Here in this code:
The output of this code will again be the same as of previous:
Hi! Shashi
You are a Faculty
You have qualified B Level
Here Hi!
(in the first line), You are
(in the second line) and You have qualified
(in the third line) are being returned as headings form the same component. Shashi
(in the first line) and Faculty
(in the second line) are coming from the state defined above.
B Level
comes from another component named Home as props.
See the code of Home component below for reference:
class Home extends Component {
render(){
return(
);
}
}
export default Home;