Stop 10 · React BasicsTopics · 7/11

State

Part of the Web Development Roadmap

In short

Props come from outside. State is the component's own memory, like what's typed in a box or whether a menu is open. Change it and React redraws that part of the screen.

State is data a component remembers between renders, such as a counter or form input. When state changes, React re-renders the component with the new value.

The jaadu mascot points at a railway display board as one tile flips to a new colour.

Think of it like this

The station display board

When a train's platform changes, the railway updates the board and everyone looking sees the new number. Nobody repaints the board by hand. In React you update state, and React repaints the screen for you.

Watch it happen

One click, one re-render

You
Counter
React
DOM

click

1/5You click the button, and Counter's addThree handler runs.

The big ideas

What you need to know

useState gives a value and a setter

const [count, setCount] = useState(0). Read count, and call setCount to change it.

Setting state re-renders

Calling the setter tells React to run your component again and update the screen with the new value.

State is a snapshot

Inside one render, count never changes. The new value shows up in the next render.

Replace, don't mutate

For objects and arrays, make a new copy with spread. Changing the old one in place won't trigger a re-render.

See it in code

Walk through the code

tsx
1import { useState } from "react";2 3export default function Counter() {4  const [count, setCount] = useState(0);5 6  function addThree() {7    setCount((c) => c + 1);8    setCount((c) => c + 1);9    setCount((c) => c + 1);10  }11 12  return <button onClick={addThree}>Clicked {count} times</button>;13}

1/3useState(0) gives the current count and a setter. It starts at 0.

The updater form (c) => c + 1 always gets the latest value, so one click adds 3. Writing setCount(count + 1) three times would add only 1.

Save yourself the pain

Common mistakes

Oops

Doing todos.push(newTodo) and then setTodos(todos).

Do this instead

Create a new array: setTodos([...todos, newTodo]). React compares references, so the same array means no re-render.

Oops

Calling setCount and expecting count to change on the very next line.

Do this instead

count is a snapshot for this render. Use the new value directly, or read it in the next render.

Your mission

Build a like button

~15 min

Quick check

count is 0. One click runs setCount(count + 1) three times. What does the button show?

Go deeper

  1. 1Build a counter with useState and buttons to increase and decrease it.
  2. 2Update arrays and objects in state by creating new copies, never by changing them directly.
  3. 3Lift state up to a shared parent when two components need the same data.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge