State management at scale
Most frontend complexity is state: what the user is looking at, what they’ve typed, what came from the server, what’s loading, and what’s out of date. Small apps survive putting it all in one place. Large apps, with many teams and screens, slow down and break in confusing ways unless each kind of state has a clear home.
The most useful step is recognizing that “state” is several different things with different owners.
Diagram
Section titled “Diagram”- Data owned by the server is fetched into a cache that tracks freshness and refetches.
- What the user is viewing lives in the URL, so it can be shared, bookmarked and restored, and keys the cache.
- Transient UI and form state stays in the component that uses it.
- Only genuinely app-wide client state, such as the session and preferences, goes in a global store.
The kinds of state
Section titled “The kinds of state”| Kind | Examples | Best home |
|---|---|---|
| Server state | Orders, account details, product data | A server-state cache such as TanStack Query (opens in a new tab), SWR, RTK Query or Apollo Client |
| URL state | Search terms, filters, current page, selected item | The URL (path and query string) |
| Local UI state | Is the menu open, which tab is active | The component |
| Form state | Field values, validation errors, dirty flags | A form library or the form component |
| Shared client state | Signed-in user, theme, feature flags, a shopping cart before checkout | A small global store or context |
Server state is the big one
Section titled “Server state is the big one”Most “global state” in large apps is really a copy of server data, and copies go stale. A server-state cache handles what hand-written stores get wrong: deduplicating requests, caching by key, refetching when data may have changed, retrying, pagination, optimistic updates, and invalidating related queries after a change. Moving server data out of a global store and into a query cache usually removes most of the store.
Principles
Section titled “Principles”- Keep state as close as possible to where it’s used. Lift it up only when two parts of the UI need it.
- Derive, don’t duplicate. Compute totals and filtered lists from source state instead of storing them separately.
- Put shareable state in the URL. If a user would expect a link or the back button to restore it, it belongs in the URL.
- One owner per piece of state, and clear boundaries between teams. In micro-frontends, each one owns its own state and shares only through explicit contracts such as URL, events or a small shell API.
- Design optimistic updates with rollback. Show the change immediately, and undo it clearly if the server rejects it.
Common pitfalls
Section titled “Common pitfalls”- One giant global store holding server data, UI flags and form drafts, where every change re-renders half the app.
- Stale data after a mutation because nothing invalidated the cached queries.
- Filters that reset on refresh because they weren’t in the URL.
- Micro-frontends sharing a global store, which couples teams as tightly as a monolith.
Related patterns
Section titled “Related patterns”- Rendering strategies
- Backend for frontend, which can shape server data for the client
- Offline-first mobile architecture
- Caching strategies
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.