Rates TodayNIFTY25,819.3+2.34%SENSEX83,938.3+2.32%Gold 24K ₹11,235.62/g down271.8Silver ₹128.69/g down5.77
InsightforGeeks
ReactJSPart of the React learning hub

React useState Explained: Updating State, Objects and Arrays

insightforgeeks-editorial
25 Nov 2026
3 min read
Share:

useState is the first hook most people learn, and the one most often used wrongly. It gives a component a piece of memory that survives re-renders and, when you change it, tells React to draw the component again. This guide shows how it works and the three mistakes that cause most bugs: changing state directly, updating from the old value, and copying objects and arrays badly.

The basics

useState takes the starting value and returns a pair: the current value and a function to change it.

import { useState } from "react";

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

  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}

Calling setCount does not change count in the code that is running now. It asks React to render the component again, and in that next render count has the new value.

Mistake 1: changing state directly

Assigning to a state variable does nothing useful, because React is never told. The screen stays the same.

// Wrong: React does not know anything changed
count = count + 1;

// Right: use the setter
setCount(count + 1);

Mistake 2: updating from the old value

State is a snapshot for each render. If you call the setter several times in one event, each call sees the same old value.

function addThree() {
  setCount(count + 1);
  setCount(count + 1);
  setCount(count + 1);
  // count goes up by 1, not 3
}

When the new value depends on the previous one, pass a function. React hands it the latest value each time.

function addThree() {
  setCount((c) => c + 1);
  setCount((c) => c + 1);
  setCount((c) => c + 1);
  // count goes up by 3
}

Objects: copy, then change

React compares the old and new value to decide whether to render. If you change an object in place, the reference is the same and the update can be missed. Create a new object instead, using the spread operator.

const [user, setUser] = useState({ name: "Asha", city: "Kochi" });

// Wrong: same object, changed in place
user.city = "Pune";
setUser(user);

// Right: a new object with one field replaced
setUser({ ...user, city: "Pune" });

Arrays: add, remove and change without mutating

Avoid push, splice and assigning by index on state. Use methods that return a new array.

const [items, setItems] = useState(["tea", "coffee"]);

// add
setItems([...items, "milk"]);

// remove one
setItems(items.filter((item) => item !== "coffee"));

// change one
setItems(items.map((item) => (item === "tea" ? "green tea" : item)));

Setting the starting value from a heavy calculation

If the first value is expensive to work out, pass a function so React runs it only once, not on every render.

const [data, setData] = useState(() => loadFromStorage());

When state should live somewhere else

  • If two components need the same value, move the state up to their closest common parent and pass it down as props.
  • If many distant components need it, use context. Our React Context guide shows how.
  • If the value can be worked out from other state, do not store it. Calculate it while rendering.

Quick checklist

  • Always change state with its setter, never by assignment.
  • Use the function form when the new value depends on the old one.
  • Copy objects and arrays; do not change them in place.
  • Keep state small. Store what you cannot calculate.

Once state is clear, the next step is learning how to run code in response to it. That is what useEffect is for.

Tags:#react#hooks#useState
Found this helpful? Share it with friends & colleagues
Share:
insightforgeeks-editorial Verified Editorial Staff

Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.

Published on 25 November 2026General Information · Disclaimer

Recommended based on your reading interest

More in ReactJS