Lesson 3 of 6By Taha Sufiyan
Last updated on Feb 12, 202110435React is the most popular front end JavaScript library today. From startups to big corporations, companies are adopting this widely used technology. Big names like Netflix, Airbnb, The New York Times, and many more are already using it on their websites and mobile applications. React’s popularity grew mainly due to how fast React web applications perform when compared to those developed using Angular. React introduced several concepts that overcame the drawbacks of previous front end frameworks.
This article will help you get familiar with a very important concept of React, the state. React State holds the data for a component. The component, in turn, returns the data contained within the state to the output.
We’ll be covering the following topics in this article:
The state is a built-in React object that is used to contain data or information about the component. A component’s state can change over time; whenever it changes, the component re-renders. The change in state can happen as a response to user action or system-generated events and these changes determine the behavior of the component and how it will render.
class Greetings extends React.Component { state = { name: "World" }; updateName() { this.setState({ name: "Simplilearn" }); } render() { return( <div> {this.state.name} </div> ) } } |
State can be updated in response to event handlers, server responses, or prop changes. This is done using the setState() method. The setState() method enqueues all of the updates made to the component state and instructs React to re-render the component and its children with the updated state.
Always use the setState() method to change the state object, since it will ensure that the component knows it’s been updated and calls the render() method.
Now that we are familiar with the concept of a state in React, let’s have a look at how it is implemented in a React web application.
class Bike extends React.Component { constructor(props) { super(props); this.state = { make: "Yamaha", model: "R15", color: "blue" }; } changeBikeColor = () => { this.setState({color: "black"}); } render() { return ( <div> <h2>My {this.state.make}</h2> <p> It is a {this.state.color} {this.state.model}. </p> <button type="button" onClick={this.changeBikeColor} >Change color</button> </div> ); } } |
Master the fundamentals of React for developing user interfaces like props and state with the React.js Training Course. Enroll now!
Let’s go through the fundamental differences between state and props:
State | Props | |
---|---|---|
Use Case | State is used to store the data of the components that have to be rendered to the view | Props are used to pass data and event handlers to the children components |
⇙Mutability | State holds the data and can change over time | Props are immutable—once set, props cannot be changed |
Component | State can only be used in class components | Props can be used in both functional and class components |
Updation | Event handlers generally update state | The parent component sets props for the children components |
And with that, we are now familiar with state, one of the essential concepts of React.
Check out the following video tutorial to get an in-depth understanding of ReactJS state -
Now that you have learned one of the React’s most fundamental concepts, you may be wondering how you can obtain the skills necessary to take advantage of its rising popularity. Simplilearn offers a comprehensive React.js Training Course that will help you become career-ready upon completion.
To learn more, check out our video lessons on ReactJS for beginners covering the fundamental concepts of React in greater depth. If you’re a web and mobile developer, React training will help you broaden your skills and your career horizons.
Name | Date | Place | |
---|---|---|---|
React.js | 14 May -4 Jun 2021, Weekdays batch | Your City | View Details |
Taha is a Research Analyst at Simplilearn. He is passionate about building great user interfaces and keeps himself updated on the world of Artificial Intelligence. Taha is also interested in gaming and photography.
React.js
Full Stack Java Developer
*Lifetime access to high-quality, self-paced e-learning content.
Explore CategoryA Beginners Guide To React Props
What is ReactJS: Introduction To React and Its Features
What is ReactJS Props: A Beginner’s Guide
Top 40 ReactJS Interview Questions and Answers in 2021
ReactJS Components: Type, Nesting, and Lifecycle
React With Redux Tutorial: Learn the Basics