codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

8 / 61 topics
1Introduction to React.js2Setting Up Your Development Environment3JSX: Basics and Syntax4Components Introduction5Functional Components6Class Components7Props: Introduction and Usage8State: Introduction and Usage9Event Handling in React
Tutorials/React.js/State: Introduction and Usage
⚛️React.js

State: Introduction and Usage

Updated 2026-04-20
3 min read

State: Introduction and Usage

Overview

In this section, we will dive into one of the core concepts of React.js: state. Understanding how to manage and use state is crucial for building interactive and dynamic user interfaces. We'll explore what state is in React, how it differs from props, and how to effectively manage state within components.

What is State?

State in React refers to any data that might change over time and affects the rendering of a component. Unlike props, which are read-only and passed down from parent components, state is owned by the component itself and can be updated using specific methods provided by React.

Key Characteristics of State

  1. Local to Component: State is local to the component where it is defined. It cannot be accessed or modified directly by other components.
  2. Mutable: Unlike props, state can be changed. When state changes, React re-renders the component to reflect these changes.
  3. Encapsulation: State encapsulates the internal logic of a component, making it easier to manage and reason about.

Managing State in Functional Components

With the introduction of Hooks in React 16.8, managing state in functional components became as straightforward as using class components. The useState hook is the primary way to add state to functional components.

Basic Usage of useState

Here's a simple example to illustrate how to use useState:

import React, { useState } from 'react';

function Counter() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Explanation

  • useState Hook: This hook returns an array with two elements: the current state value (count) and a function that lets you update it (setCount). You can call this function from event handlers or other places to change the state.
  • Initial State: The initial state is passed as an argument to useState. In this example, the initial count is set to 0.

Updating State

When you call setCount, React schedules a re-render of the component with the updated state. It's important to note that state updates are asynchronous, meaning they don't happen immediately but rather batched for performance reasons.

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

  // Correct way to update state based on previous state
  function incrementCount() {
    setCount(prevCount => prevCount + 1);
  }

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={incrementCount}>
        Click me
      </button>
    </div>
  );
}

Best Practices

  • Avoid Direct Mutation: Always use the state updater function (e.g., setCount) to update state. Directly mutating state can lead to unpredictable behavior.
  • Functional Updates: When updating state based on its previous value, always use functional updates. This ensures that you are working with the most recent state.

State and Side Effects

In React, side effects such as data fetching, subscriptions, or manually changing the DOM should be handled using the useEffect hook. This hook allows you to perform actions after rendering and clean up those actions when necessary.

Basic Usage of useEffect

Here's an example of a timer component using useEffect:

import React, { useState, useEffect } from 'react';

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    // Set up a timer to update state every second
    const interval = setInterval(() => {
      setSeconds(seconds => seconds + 1);
    }, 1000);

    // Clean up the interval when the component unmounts
    return () => clearInterval(interval);
  }, []); // Empty dependency array means this effect runs only once after the initial render

  return (
    <div>
      <p>Seconds: {seconds}</p>
    </div>
  );
}

Explanation

  • useEffect Hook: This hook takes a function that contains side effects and an optional array of dependencies. The function runs after every render by default, but you can control it with the dependency array.
  • Cleanup Function: Returning a function from useEffect allows you to clean up resources when the component unmounts or before the effect runs again.

Best Practices

  • Dependency Array: Always include all variables used inside the effect in the dependency array. This ensures that the effect re-runs whenever those dependencies change.
  • Avoid Infinite Loops: Be cautious with how you use state and props inside useEffect to avoid creating infinite loops.

Conclusion

State management is a fundamental aspect of building dynamic React applications. Whether you're using functional components with Hooks or class components, understanding how to manage state effectively will help you create interactive and responsive user interfaces. Always remember to update state immutably and use lifecycle methods appropriately to ensure your components behave as expected.


PreviousProps: Introduction and UsageNext Event Handling in React

Recommended Gear

Props: Introduction and UsageEvent Handling in React