Cover Image for ReactJS State
120 views

ReactJS State

The ReactJS state is a fundamental concept used to manage and store data that can change over time within a component. React components can have their own state, which allows them to be dynamic and interactive. Here are the key aspects of React state:

  1. Class Components and State:
  • In class components, state is typically initialized and managed using the constructor and the setState method.
  • State is declared as an object in the component’s constructor, and you can initialize it with default values.
TypeScript
 import React, { Component } from 'react';

 class MyComponent extends Component {
   constructor(props) {
     super(props);
     this.state = {
       count: 0,
       name: 'John',
     };
   }

   render() {
     return (
       <div>
         <p>Count: {this.state.count}</p>
         <p>Name: {this.state.name}</p>
       </div>
     );
   }
 }

 export default MyComponent;
  1. Functional Components and State (useState Hook):
  • In functional components, state can be managed using the useState hook, which was introduced in React 16.8.
  • The useState hook returns an array with two elements: the current state value and a function to update it.
TypeScript
 import React, { useState } from 'react';

 function MyComponent() {
   const [count, setCount] = useState(0);
   const [name, setName] = useState('John');

   return (
     <div>
       <p>Count: {count}</p>
       <p>Name: {name}</p>
     </div>
   );
 }

 export default MyComponent;
  1. Updating State:
  • In class components, you should use the setState method to update state. It accepts an object that specifies the new state values.
  • In functional components with the useState hook, you use the state updater function returned by useState to update state.
TypeScript
 // Class Component
 this.setState({ count: this.state.count + 1 });

 // Functional Component
 setCount(count + 1);
  1. Asynchronous State Updates:
  • State updates in React are asynchronous to improve performance.
  • If you need to perform an action immediately after a state update, you can pass a callback function as the second argument to setState.
TypeScript
 this.setState({ count: this.state.count + 1 }, () => {
   console.log('State updated');
 });
  1. Immutable State:
  • State should not be mutated directly; instead, create a new state object based on the current state when making updates.
TypeScript
 // Incorrect (mutating state directly)
 this.state.count++; // Avoid this

 // Correct (creating a new state object)
 this.setState({ count: this.state.count + 1 });
  1. Functional State Updates:
  • In functional components with the useState hook, you can use the previous state value to update state functionally.
TypeScript
setCount((prevCount) => prevCount + 1);
  1. State and Rendering:
  • When state is updated, React re-renders the component to reflect the new state.
  • The render method (in class components) or the component function (in functional components) is called to determine the UI.

React state is a crucial concept for building dynamic and interactive user interfaces. It allows components to respond to user interactions, display data, and manage the application’s state effectively. Understanding how to use and update state is fundamental to working with React.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS