Skip to main content
⚑ OpenDevDocs
React
frontendreactGuideBeginner

React State and Component Memory

Understanding component state in React: useState hook, immutability rules, state updates, and lifting state up.

Verified: 2026-09-30
Tested on:react 19.x
Platforms:
web
#react#state#use-state#reactivity#memory

React State and Component Memory

State is a component's internal memory. Unlike regular JavaScript variables that reset on every function execution, React preserves state across re-renders and automatically updates the DOM whenever state changes.


1. Declaring State with useState

import { useState } from "react";

export function Counter() {
  // [currentValue, setterFunction] = useState(initialValue)
  const [count, setCount] = useState<number>(0);

  function handleIncrement() {
    // Updater function ensures correct state during rapid updates:
    setCount(prev => prev + 1);
  }

  return (
    <div>
      <p>Current Count: {count}</p>
      <button type="button" onClick={handleIncrement}>
        Increment
      </button>
    </div>
  );
}

2. The Golden Rule of State: Never Mutate Directly!

State in React must be treated as immutable. Never modify properties directly on state objects or arrays:

// ❌ WRONG: Mutating state directly does NOT trigger re-render!
// user.name = "Alex";
// setUser(user);

// βœ… CORRECT: Create a new object snapshot using spread operator:
setUser(prevUser => ({
  ...prevUser,
  name: "Alex",
}));

// Updating an array immutably:
setItems(prevItems => [...prevItems, "New Item"]);

3. Lifting State Up

When two or more sibling components need to share or coordinate the same state, move the state up to their closest common parent component and pass it down via props.


On this page