DEV Community

Cover image for Simplified Guide to State in react for Beginners
Hassan Shahzad Aheer
Hassan Shahzad Aheer

Posted on

Simplified Guide to State in react for Beginners

Introduction:

React, a popular JavaScript library for building user interfaces, introduces the concept of "state" to create dynamic and responsive components. In this concise guide, we'll explore the fundamental aspects of state in React to help you grasp the concept in just 30 seconds or less.

1. What is State in React?

Concept: State in React is a built-in feature that allows components to manage and store their own data. It represents the current state of a component's dynamic information.

Use Case: It's used for keeping track of things that can change during a component's lifecycle, such as user input, toggles, and counters.

2. Why Do We Use State?

Purpose: State enables React components to be dynamic and responsive to user interactions. It allows for real-time updates and re-rendering when data changes, providing a smooth user experience.

Advantages: React state simplifies UI updates, improves user interaction, and helps in building complex, interactive web applications.

3. How Does State Work?

Mechanism: React components can declare and manage state using the useState hook (in functional components) or the this.state object (in class components).

Updating State: To change the state, you use state-modifying functions (e.g., setState in class components or the updater function in useState), providing the new state value.

4. State vs. Props

State: Used to manage and store data that belongs to a component itself and can change.

Props: Used for passing data from parent to child components and should be considered immutable.

5. Modifying State to Make Changes

Process: To update state, use the state-modifying function provided by React (e.g., setState or the updater function from useState). Pass in the new state value or a function that calculates it based on the previous state.

Code Examples:

Using State in a Functional Component

import React, { useState } from 'react';

function Counter() {
   const [count, setCount] = useState(0);

   const increment = () => {
      setCount(count + 1);
   };

   return (
      <div>
         <p>Count: {count}</p>
         <button onClick={increment}>Increment</button>
      </div>
   );
}
Enter fullscreen mode Exit fullscreen mode

Using State in a Class Component

import React, { Component } from 'react';

class Counter extends Component {
   constructor() {
      super();
      this.state = {
         count: 0,
      };
   }

   increment = () => {
      this.setState({ count: this.state.count + 1 });
   };

   render() {
      return (
         <div>
            <p>Count: {this.state.count}</p>
            <button onClick={this.increment}>Increment</button>
         </div>
      );
   }
}
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate the practical use of React state to manage and update data within components.

Summary:

  • State in React allows components to manage dynamic data.
  • Its purpose is to make components interactive and responsive.
  • The mechanism includes declaring state and using state-modifying functions.
  • State vs. Props distinguishes between component-internal data (state) and data passed from parent components (props).
  • Modifying State involves using state-modifying functions to update the state based on its previous value.

With this quick guide, you're well on your way to understanding the essential concept of state in React.


Follow me on Twitter, LinkedIn, and Instagram for more web development insights and updates!

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

Nice Effort