Sub topic 1
State is a snapshot, not a variable you edit
Prerequisites
This chapter assumes you know:
- JavaScript objects, arrays, and the spread operator.
- Basic React function components and the
useStatehook. - How to handle events such as clicks in React.
You do not need to know array methods like map and filter yet. They appear in later chapters.
State looks like a variable, but it isn't
When you write const [count, setCount] = useState(0), count looks like a variable that setCount changes. That picture is wrong, and it causes most of the bugs in this tutorial.
Each time React renders your component, it calls your function once. React gives that call a fixed value for each piece of state. That value is a snapshot: the state as it was for that one render. Everything created during the render uses that snapshot. This includes the JSX and the event handlers.
Calling a setter does not edit the snapshot. It asks React to render again, and the next render gets a new snapshot.
A worked example
Read this component and predict what happens when you click once.
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
console.log('count in this handler:', count);
}
return <button onClick={handleClick}>Clicked {count} times</button>;
}After one click, the console prints count in this handler: 0. The button then reads "Clicked 1 times", not 3.
In this render, count is 0. So the handler contains setCount(0 + 1) three times. Each call asks for the same next value, 1. The console.log runs after the setter calls, but it still prints 0 because the snapshot has not changed.
The sequence of events
The handler from render 1 never sees the value 1. Only the function that React calls for render 2 does.
What this means for objects and arrays
Objects and arrays follow the same rule. When state holds an object, the object in a snapshot is something you read from, not something you edit. To change what appears on screen, you give the setter a new value, and React renders that value as the next snapshot.
Here are two ways to think about the difference:
| Mental model | What you do | Result |
|---|---|---|
| State is a variable | Change it in place | React is not told anything changed |
| State is a snapshot | Pass a replacement to the setter | React renders a new snapshot |
The next chapter shows exactly why editing in place fails. The chapters after it show how to build replacements for objects and arrays.
Common mistakes with snapshots
- Reading state right after setting it. The variable still holds the old value until the next render.
- Expecting repeated setter calls to stack.
setCount(count + 1)three times in one handler adds 1, not 3. Updater functions, covered later, fix this. - Treating the setter as an assignment.
setCount(5)is a request to render with 5, notcount = 5.
Exercise
A component has const [score, setScore] = useState(10). A click handler runs setScore(score + 5) and then console.log(score). What does the console print, and what will score be on the next render?
Show answer
The console prints 10, because the handler uses the snapshot from the current render and the setter does not change it. On the next render score is 15, because React renders again with the requested value.