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

20 / 61 topics
18Context API Basics19Hooks: Introduction and Overview20Using the useState Hook21Using the useEffect Hook22Creating Custom Hooks23Memoization in React
Tutorials/React.js/Using the useState Hook
⚛️React.js

Using the useState Hook

Updated 2026-04-20
3 min read

Introduction

In this section, we will dive deep into one of the most fundamental hooks provided by React, the useState hook. This hook allows you to add state management capabilities to functional components, which were previously only possible with class components. Understanding how to use useState effectively is crucial for building dynamic and interactive user interfaces in React.

What is useState?

The useState hook is a built-in React hook that lets you add React state to function components. It returns an array containing the current state value and a function that lets you update it. By using this hook, you can manage local component state without converting your component into a class.

Basic Syntax

import React, { useState } from 'react';

function ExampleComponent() {
  const [state, setState] = useState(initialState);

  return (
    <div>
      {/* Render the current state */}
      <p>Current State: {state}</p>
      {/* Update the state */}
      <button onClick={() => setState(newState)}>Update State</button>
    </div>
  );
}

Key Points

  • Initial State: The initial state is passed as an argument to useState. This value will be used when the component is first rendered.
  • State Variable: The first element of the array returned by useState is the current state variable. You can use this variable to display or manipulate the state.
  • State Setter Function: The second element is a function that allows you to update the state. When called, it schedules an update to the component's state and triggers a re-render.

Initializing State

The initial state passed to useState can be any valid JavaScript value, including numbers, strings, objects, arrays, or even functions.

Example: Numeric State

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

Example: Object State

function UserProfile() {
  const [user, setUser] = useState({
    name: 'John Doe',
    age: 30,
  });

  return (
    <div>
      <p>Name: {user.name}</p>
      <p>Age: {user.age}</p>
      <button onClick={() => setUser({ ...user, age: user.age + 1 })}>Birthday</button>
    </div>
  );
}

Updating State

When updating state, it's important to understand how React handles state updates.

Functional Updates

For complex state logic that involves the previous state, you can use a function in your updater. This ensures that the update is based on the most recent state value.

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(prevCount => prevCount + 1)}>Increment</button>
    </div>
  );
}

Batched Updates

React batches multiple state updates into a single re-render for performance optimization. This means that if you call setState multiple times in quick succession, React will only re-render once.

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => {
        setCount(count + 1);
        setCount(count + 1); // This will only increment once
      }}>Increment Twice</button>
    </div>
  );
}

Best Practices

Use Multiple State Variables

It's perfectly fine to use multiple useState calls in a single component. Each call is independent and does not affect the others.

function ProfileForm() {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');

  return (
    <form>
      <input type="text" value={name} onChange={(e) => setName(e.target.value)} />
      <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
    </form>
  );
}

Avoid Direct State Mutation

Never mutate the state directly. Instead, always use the state setter function to update the state.

// Incorrect
user.age = user.age + 1;

// Correct
setUser({ ...user, age: user.age + 1 });

Use Functional Updates for Complex Logic

For complex state updates that depend on the previous state, always use functional updates.

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(prevCount => prevCount + 1)}>Increment</button>
    </div>
  );
}

Advanced Usage

Lazy Initialization

If the initial state is computationally expensive to calculate, you can pass a function to useState that returns the initial state. This function will only be called once during the initial render.

function HeavyComponent() {
  const [data, setData] = useState(() => {
    // Expensive computation here
    return computeExpensiveData();
  });

  return (
    <div>
      {/* Render data */}
    </div>
  );
}

State Reset

You can reset the state to its initial value by calling the setter function with the initial state.

function ToggleButton() {
  const [isOn, setIsOn] = useState(false);

  return (
    <div>
      <p>Toggle is {isOn ? 'ON' : 'OFF'}</p>
      <button onClick={() => setIsOn(!isOn)}>Toggle</button>
      <button onClick={() => setIsOn(false)}>Reset</button>
    </div>
  );
}

Conclusion

The useState hook is a powerful tool for managing state in React function components. By understanding how to initialize, update, and manage state effectively, you can build more dynamic and interactive user interfaces. Remember to follow best practices such as using multiple state variables, avoiding direct mutation, and leveraging functional updates for complex logic.

In the next section, we will explore other advanced hooks provided by React, including useEffect, which allows you to perform side effects in function components. Stay tuned!


PreviousHooks: Introduction and OverviewNext Using the useEffect Hook

Recommended Gear

Hooks: Introduction and OverviewUsing the useEffect Hook