Skip to content
Core ConceptsChapter 8 / 37Beginner4 min read

State with useState

Learn how to create and update component state with the React useState hook, including re-renders, functional updates and immutable objects.

On this page

1. What is state with useState?

A normal variable is created again on every render, and changing it does not update the screen. State is remembered between renders, and when you set it, React renders the component again.

2. Why does it matter?

Normal variableuseState
Value resets on every renderValue is remembered between renders
Change the value, the screen stays the sameSet it, and React re-renders
React does not know about the changeReact tracks the change
You cannot build an interactive UICounters, toggles and forms all become possible

3. Where is it used?

Use caseWhat to keep in state
Counter / likesA number
Modal open/closeA boolean
Form inputA string
Todo listAn array of objects
Selected tabA string or an id

4. How does it work?

useState(initialValue) returns an array: the current value and a setter function.

The state update cycle
The user clicks a button
The setter function is called with a new value
React schedules a re-render
The component runs again and gets the new state
The new JSX appears on the screen

5. Code example

Counter

jsx
import { useState } from "react";

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

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

Functional update

When the new value depends on the previous value, pass an updater function:

jsx
function handleTripleClick() {
  setCount((c) => c + 1);
  setCount((c) => c + 1);
  setCount((c) => c + 1);
}

Updating object state

jsx
const [user, setUser] = useState({ name: "Ali", city: "Lahore" });

function moveToKarachi() {
  setUser({ ...user, city: "Karachi" });
}

How does the code work?

PartMeaning
useState(0)Initial value is 0, used only on the first render
[count, setCount]Array destructuring: the value and the setter
setCount(count + 1)Sets the new value and triggers a re-render
setCount((c) => c + 1)Calculates from the latest queued value, so running it three times gives +3
{ ...user, city }Creates a new object instead of mutating the old one

6. Common mistakes

Mistake: Mutating state directly, such as assigning to user.city

Why? The object stays the same, so React does not notice the change and does not re-render.

Better: Always create a new object or array. Use immutable patterns like spread, map and filter.

Mistake: Logging state right after calling the setter and expecting the new value

Why? State is a snapshot, so the current render still holds the old value.

Better: If you need the new value now, calculate it in a variable first, or use it in the next render.

Mistake: Writing count + 1 directly in an update that depends on the previous value

Why? When several updates run in one event, they all use the same old value and the result is wrong.

Better: Write an updater function: setCount(c => c + 1).

Mistake: Turning a value that can be calculated into state as well

Why? You have to keep two states in sync, and that leads to bugs.

Better: Calculate derived values during render, for example fullName = first + last.

Interview tip

"Why does a state update feel async?" Answer: React batches updates and gives you the new value after the render. Each render sees one snapshot of state.

7. What to learn before and after

Final quick revision

PointDetail
useStateCreates memory for the component
Return value[value, setValue]
SetterSets a new value and triggers a re-render
SnapshotThe value does not change inside the current render
Objects/arraysAlways make a new copy