State is a snapshot
React chapter 138 · useState · the update queue · batching · source: react.dev/learn/state-as-a-snapshot
The problem
A “+3” button calls setScore(score + 1) three times and the score goes up by one. Inside one render, score is a fixed number, so all three calls ask for the same value.
The pattern that fixes it
Pass an updater: setScore(s => s + 1). React queues the functions and runs them in order on the next render, each getting the result of the one before. Three updates, one render, +3.
In plain words
Each render gets its own copy of the state, frozen like a photo. Calling setScore does not change the photo you are holding; it asks React for a new photo next render.
Three people each read “score: 0” off the same whiteboard photo and each write “make it 1”. Passing a note that says “add one to whatever it is now” works no matter how many people do it.
Terms
- snapshot
- The state values a render was called with. Event handlers created in that render see those values forever.
- update queue
- React stores every setState call and applies them, in order, during the next render.
- batching
- Several updates in one event produce one re-render, not one per call.
Going deeper
Value vs updater, precisely
setScore(5) queues “replace with 5”. setScore(s => s + 1) queues a function. During the next render React folds the queue left to right: 0 → replace 1 → replace 1 is 1, 0 → +1 → +1 → +1 is 3. Use the updater whenever the next value depends on the previous one.
Stale closures
A setTimeout, promise or subscription callback created in render N keeps render N’s snapshot. Three clicks within two seconds each schedule setScore(score + 1) with the same old score. Updaters, refs for “latest value”, or useEffectEvent (chapter 15) fix the different shapes of this bug.
Automatic batching (React 18+)
Updates are batched everywhere: event handlers, timeouts, promises, native listeners. flushSync(() => set(x)) forces a synchronous render when you must read the DOM right after, for example to scroll to a new item. It is an escape hatch, not a habit.
Keep learning
- Previous: Render, then commit
- Next: Effects: sync, then clean up
- Related: Render, then commit
- Related: Effects: sync, then clean up
- Related: Identity: position, type, key
- Related: Lift state up
- Related: useRef: remember without re-rendering
Overview
Update queue for the last click score + 1 Press +3 to see the update queue. Real life: a game score, a cart quantity, a like counter — anything that adds to itself.
Three requests, all for the same value.
Press +3. The score rises by 1, and “score read right after” is still the old number. Press +1 in 2 s three times quickly. Every timeout carries the same old snapshot: +1, not +3. Value updates versus updater functions in the queue setScore(score + 1) ×3 replace → 1 setScore(s ⇒ s + 1) ×3 base state Both queues are applied in ONE re-render (batching). Same button. Queue functions, not values. How React applies the update queue The click handler runs inside the render where score is 0. Every mention of score in it is 0. Each setScore(score + 1) queues the same instruction: “replace with 1”. On the next render React applies the queue: 1, then 1, then 1. Result: 1. With an updater, the queue holds functions instead of values. Update style score + 1
Next value depends on the previous one? Use the updater.
The renders counter goes up by one per click, not three: React batched the three updates.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.