The Web FrontendDeeper

Kayavak / The Web Frontend / FE·25 State Management / Deeper

Deeper · The Web Frontend

State Management

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

When React made components the unit of UI, a new question appeared: where does data live when many distant components need it? Facebook's Flux architecture (2014) answered with one-directional flow through a dispatcher to stores. Redux (Dan Abramov and Andrew Clark, 2015) simplified it into one store, updated only by pure reducer functions responding to actions, which made every change traceable and enabled "time-travel" debugging. For a few years nearly every React app put nearly everything in Redux.

Then people noticed that most of that state was a copy of something on a server, with all the problems of caching: staleness, refetching, deduplication, errors. Libraries such as React Query (TanStack Query, 2019–20) and SWR treated server state as a cache with its own rules, and client stores shrank to what genuinely belonged to the browser. For state shared between devices or people, CRDTs (formalised by Marc Shapiro and colleagues in 2011) let copies merge without a central referee.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Before choosing a tool, ask who owns the truth. Most "state management" bugs come from keeping a second copy of something whose truth lives elsewhere: on the server, in the URL, or in another component.

Question Answer
What is it? Deciding where each kind of data lives in a frontend, who may change it and how changes propagate
Problem it solves Keeping many components consistent without tangling them together
Parts UI state, form state, shared client state, server state (a cache), URL state, persistent state
How it works Each kind goes in its natural home: local state, a store, a query cache, the address bar, storage
Assumptions Each piece of truth has exactly one owner; everything else derives from it
When they change Duplicated or derived data stored separately drifts out of sync
Where it fails Offline and collaborative apps, where several copies must legitimately diverge and later merge
Related ideas Caching (ch19); UI architecture (ch21); consistency models (Distributed Systems)

What if?

What if you put server data in a client store as if it were yours?

Reason it through, then open

You'd write by hand what a cache library provides: when to refetch, how to deduplicate requests from two components, what to show while loading, how to invalidate after a mutation, and how to recover from errors. And the copy would silently go stale when someone else changed the data. Server state is a cache; treat it like one.

What if two people edit the same document offline?

Reason it through, then open

When they reconnect, "last write wins" throws one person's work away. Locking would have stopped them working offline at all. CRDTs and operational transformation merge concurrent edits so both survive and every copy converges to the same result, at the cost of more complex data structures and metadata.

Where the boundary is

Aspect Client state Server state
Owned by This browser A server (shared with others)
Examples Open menus, form drafts, theme User records, orders, search results
Can be stale? No: it's the truth Always potentially
Tool Local state, a store A query cache with invalidation

How sure are we?

Claim How it's known Where from
Redux was created in 2015, inspired by Flux and Elm Historical Redux documentation
CRDTs converge without coordination Theorem Shapiro et al., 2011, under their stated conditions
Most frontend state is server state Model Common experience in data-driven apps; not true of editors or games

Still open

Local-first software (apps that keep the primary copy on the device and sync, often with CRDTs) is an active movement since a 2019 essay by Ink & Switch; how well it scales to large datasets and permissions is still being worked out.

What would change your mind? If server-driven UI (where the server sends updated HTML) covered most apps, client state management would shrink to almost nothing. It covers many, not most interactive ones.

Recall

What question comes before choosing a state tool?

Who owns the truth for this data?

Why is server state different from client state?

It's a cache of data someone else owns, so it can always be stale and needs refetching and invalidation.

What do CRDTs guarantee?

Concurrent copies merge and converge to the same state without a central coordinator.

Why not store derived data?

It's a second copy that can drift from its source; compute it instead.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.