React States - Day Six

Introduction

In the sixth part of my React article series, we are going to learn what is State and how to use it in the React Component. Before moving to components, please read my previous articles.

State

In React, each component has a state. A state is an object with values. It determines a component's rendering and behavior. It is used when a component needs to change independently of its parent. Components with the state have more complexity.

React
Note - Image Source Google

Defining the state in React component.

  1. constructor(props) {  
  2.     super(props);  
  3.     this.state = {  
  4.       articleName: "What is State in React?"  
  5.     }  
  6.  }  

Accessing a state via the state property and supplied as attribute.

  1. <Article articleName={this.state.articleName}/>  

As in the above screenshot, we have to pass the props as the attributes in elements, i.e., ‘articleName’.

Following are the steps to use States in React Component

  1. Create a React application using ‘create-react-app app-name’.
  2. After creating the application, open the src/App.js file. Paste the following code in that file.
    1. import React, { Component } from 'react';  
    2. import logo from './logo.svg';  
    3. import './App.css';  
    4.   
    5. class App extends Component {  
    6.   constructor(props) {  
    7.     super(props);  
    8.     this.state = {  
    9.       articleName: "What is Props in React?"  
    10.     };  
    11.     this.handleClick = this.handleClick.bind(this);  
    12.  }  
    13.  handleClick(){  
    14.    this.setState({  
    15.     articleName: "What is State in React?"  
    16.    });  
    17.  }  
    18.   render() {  
    19.     return (  
    20.       <div className="App">  
    21.         <header className="App-header">  
    22.           <img src={logo} className="App-logo" alt="logo" />  
    23.           <h1 className="App-title">Welcome to C# Corner</h1>  
    24.         </header>  
    25.           <Article articleName={this.state.articleName}/>  
    26.           <Author authorName="Jeetendra"/>                  
    27.           <button onClick={this.handleClick}> Click to Change State </button>  
    28.       </div>  
    29.     );  
    30.   }  
    31. }  
    32.   
    33. const Article = (props) =>{  
    34.   return(  
    35.     <h1 className="App-intro">{props.articleName}</h1>  
    36.   );  
    37. }  
    38.   
    39. class Author extends React.Component {     
    40.   render(){  
    41.     return(  
    42.       <h1 className="App-intro">By {this.props.authorName}</h1>  
    43.     );  
    44.   }  
    45. }  
    46.   
    47. export default App;  
  1. Finally, we have done with the changes. Now, the next step is to run the application using the command ‘npm start’.
  2. Next, after running the command, the application is opening in the browser, as in the following screenshot.

    React

You can see the output in the browser with one button text ‘Click to change State’. Click on that button and it changes the output from the above to the below one.

React

We have changed the previous state's value to the new state using the ‘setState’ function. It is used for updating the state values. In the below code, you can see that we have changed the state on that basis of ‘onClick’ event and used the ‘setState’ function in ‘handleClick’ function.

  1. class App extends Component {  
  2.   constructor(props) {  
  3.     super(props);  
  4.     this.state = {  
  5.       articleName: "What is Props in React?"  
  6.     };  
  7.     this.handleClick = this.handleClick.bind(this);  
  8.  }  
  9.  handleClick(){  
  10.    this.setState({  
  11.     articleName: "What is State in React?"  
  12.    });  
  13.  }  
  14.   render() {  
  15.     return (  
  16.       <div className="App">  
  17.         <header className="App-header">  
  18.           <img src={logo} className="App-logo" alt="logo" />  
  19.           <h1 className="App-title">Welcome to C# Corner</h1>  
  20.         </header>  
  21.           <Article articleName={this.state.articleName}/>  
  22.           <Author authorName="Jeetendra"/>                  
  23.           <button onClick={this.handleClick}> Click to Change State </button>  
  24.       </div>  
  25.     );  
  26.   }  
  27. }  

Finally, we have used ‘state’ property ‘articleName’ in the React component and updated it ‘onClick’ using the ‘setState’ function. Also, you can send the ‘prevState’ as well for this ‘setState’ function.

Summary

I hope that beginners, as well as students, now understand what a State is and how to use it in the React Component. If you have any suggestions regarding this article, please contact me. 

Stay tuned for other concepts of React.

“Learn It, Share it.”

<= Previous Article  Next Article =>