1. Home
  2. Training Library
  3. Programming
  4. Courses
  5. React - Adding and Updating State to Components

Updating State in Function Components

Developed with
QA

The course is part of this learning path

ReactJS: Zero to Hero
course-steps
14
certification
7
lab-steps
3
description
1
play-arrow
Updating State in Function Components
Overview
DifficultyBeginner
Duration9m
Students121
Ratings
5/5
starstarstarstarstar

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

State is often dated in an application due to some event. Without user interaction or an external influence. A really important concept to mention right from the off here, is that you should never directly mutate the value of state. If it needs to be updated, this should be done using the updater of function defined when state was declared. Why? Calling the updater to function triggers a re-render of the component that owns the state. The different algorithm will detect that the value of state has changed and then cascade this change down to every component that uses the state value. The only component will obviously be re-render, but any component that receives the value as part of its props will to mutating the value directly bypasses this process. I should not be done for this reason. We'll cover events in a lot more detail when we're looking to invest data flow, but for now it's sufficient for us to know that we can use any of the events from the API as long as we camel case them. This is for JSX interpretation to distinguish them from HTML attributes. Most will work without camel casing, but there'll be a friendly warning on the Console if you do this. You can see in the code for the component with state, we have added a button on line 11 that has an Onclick event. We supplied the Onclick value as a JSX expression. In this case, we define a callback hourly function that calls set camp with an argument of count plus one clicking on the button in the browser. You'll say expected result. The value displayed increases by one. If we inspect the React Developer tools, we can see that the value in the hooks State increases when the button is clicked. We should also note, that the countUser component updates it's display as the value supplied to it, is props. Either count state changes when ComponentWithState is re-rendered using the new value of count.

Lectures

About the Author
Students3001
Labs8
Courses29
Learning paths6

An outstanding trainer in software development with more than 15 years experience as a Corporate and Apprentice Trainer, ICT Teacher and Head of Department, with a passion for technology and its uses. Continuing to develop existing and new skills and courses, primarily in web design using PHP, JavaScript, HTML, CSS and SQL but also OOP (Java), programming foundations (Using Python), DevOps (Git, CI/CD, etc) and Agile/Scrum. Practically minded, a quick learner and a problem solver with an attention to detail to ensure high quality outcomes.

Covered Topics