image
Adding State to Class Components
Start course
Difficulty
Beginner
Duration
9m
Students
990
Ratings
4.2/5
starstarstarstarstar-border
Description

This module looks at how to add and update state to a variety of different components.

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • How to add and update state to class and function components

Intended Audience  

This Learning Path is aimed at all who wish to learn how to use the ReactJS framework.  

Prerequisites

It is essential you understand the face of contemporary web development to attend this course. We insist upon JavaScript experience, along with good HTML and CSS skills. 

Feedback 

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think. 

Transcript

Before Reaction version 16.8, state could only be added to class components. Although the advice to make all new components as functional components, there may well be situations where you come across legacy code. In anticipation of this, we'll look at how state was added to the class component. A simple class component has to have a render method that returns the representation of the UI that the component displays. 

When adding state to a class component, a constructor must be defined and it calls the super method which in turn calls the component class constructor. If props are to be used in this component, props must be passed to the constructor and the super call. This can be seen on line six and seven of the code shown. State is initialized in the constructor by setting it as an object that has keys at the name of the states to be set and their initial value. You can see in the code on line 10 that we have set the count state initially to five. 

State is accessed in the component by using this dot state dot state name notation as shown on line 19, when rendering the value of count held in state. This also rings true when passing the state value as a prop to a sub-component as on line 21. The functionality of this class component in the browser is exactly the same as we showed in the example for the function component. 

In the developer tools in the browser console, we see slightly different information than when rendering a function component. Rather than seeing a section on hooks with state values, we see a section called state and the key value pairs used. The use and effective state on the count user component is unchanged. Facebook have said they will never deprecate class components. So there is no need to convert all class components in an older application to function components, that's unless the component has been substantially rewritten.

Lectures

About the Author
Students
20468
Labs
6
Courses
29
Learning Paths
14

Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.

Previous roles have included being a Delivery Manager, Trainer, ICT teacher, and Head of Department. Ed continues to develop existing and new courses, primarily in web design using: PHP, JavaScript, HTML, CSS, SQL, and OOP (Java), Programming Foundations (Python), and DevOps (Git, CI/CD, etc). Ed describes himself as practically minded, a quick learner, and a problem solver who pays great attention to detail. 

Ed’s specialist area is training in Emerging Technologies, within Web Development. Ed mainly delivers courses in JavaScript covering vanilla JS, ES2015+, TypeScript, Angular, and React (the latter is authored by Ed) and has delivered on behalf of Google for PWAs. Ed has also developed a new suite of PHP courses and has extensive experience with HTML/CSS and MySQL. 

Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.

Covered Topics