Skip to main content

React JS State

React JS State

 Hello. Welcome to the home of developers where we discuss all about coding. If you are new here, please subscribe to our YouTube channel for the video tutorial of this content. This tutorial is part of the React JS series and today, we will discuss about STATES. In our previously article, we talked about React JS props or properties.

You should understand that React JS props are read only. Therefore, changing the value will display an error.

This is therefore where React JS States come in to store the values of the props but allowing possibility to change the state using a method.

Let us see how we can create React JS state, use it and update the state. When creating a state, have in mind that React JS state is an object.

class Hello extends React.Component {
constructor (props) {
super (props);
this.state = {
first_name : 'trycoder',
last_name : 'blog'
}
}
render() {
return // code here
}
}

Adding State in the class component requires passing props in the constructor () method and super () function.

Access the information passed in the state is done by simple calling the state object dot the property. For example;

this.state.first_name

The code above will display the value of the first name.

Note that, a function component can not use state or have its own state except through hooks which will be covered in subsequent tutorials.

Let us create and use our state using a class component in our create-react-app boiler-plate code and see the output. We will create a Hello component in a new file called Test.js

Modify the file index.js to be as follows and create a new file called Test.js and add code as seen below.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Hello from './Test'
ReactDOM.render(, document.getElementById('root'));

Test.js

React JS State
OUTPUT

React JS State
Changing State Values

To change the value of a property in the state object, we will use the setState( ) method. When ever the value of state is changed, the component re-renders. 

The code below updates the first name using setState() after a button click.

Test.js

React JS State


OUTPUT

React JS State


React JS State


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...