[FEE-600] State Management Overview
INFO
This category covers how to organize, access, and synchronize the data that drives your UI -- from a single boolean toggle to complex multi-source server data.
Context
Modern frontend UIs are functions of underlying data: form inputs, authentication tokens, cached API responses, URL parameters, animation progress. The formula UI = f(state) is easy to write and hard to run. In practice, state arrives from multiple asynchronous sources (user events, network responses, web sockets, browser APIs), must remain consistent across many components, and often needs to survive navigation, tab switches, and offline periods.
The real difficulty is the dependency chain, and the chain exists because state depends on state. A loading spinner depends on a fetch request; the fetch request depends on a URL parameter; the URL parameter depends on a user action; the user action depends on authentication state. When any link in that chain updates incorrectly, the UI becomes inconsistent, and users lose trust.
State Taxonomy
Not all state is created equal. Recognizing the kind of state you are dealing with is the first step toward choosing the right tool:
| Category | Description | Examples | Typical Tool |
|---|---|---|---|
| Local (UI) State | Belongs to a single component | Toggle open/closed, input value, hover | useState, ref() |
| Derived State | Computed from other state | Filtered list, total price, full name | useMemo, computed() |
| Shared State | Needed by multiple components | Theme, locale, shopping cart | Context, Zustand, Pinia |
| Server State | Async data owned by the backend | User profile, product list, notifications | TanStack Query, SWR |
| URL State | Encoded in the URL | Search query, pagination, filters | Router params, useSearchParams |
| Machine State | Finite, explicit transitions | Multi-step form, auth flow, media player | XState, useReducer |
Each category has different ownership, update sources, and lifecycle. Treating server state the same as local UI state, or stuffing everything into a global store, is a common source of over-engineering.
Tools Map to State Types
The taxonomy table above already answers "which state, which tool." Most applications use several categories at once: useState for local state, TanStack Query for server data, URL parameters for shareable filters. These tools are orthogonal; none is an advanced version of another.
A global store (Zustand, Pinia, Redux Toolkit) is for client state that several components genuinely share. A state machine (XState) is for flows whose valid transitions can be enumerated. You don't "graduate" from useState to TanStack Query or XState; they solve different problems.
Design Thinking
Why State Management Is Hard
Three forces make state management uniquely difficult:
Multiple async sources. User interactions, API responses, web socket messages, browser events (resize, online/offline), and timers all produce state updates. These sources operate on different timescales and can arrive in any order.
Consistency across the component tree. When a user logs out, every component showing user-specific data must update. When a product is added to a cart, the header badge, the cart drawer, and the checkout page must all reflect the change. Inconsistency is immediately visible and erodes user trust.
The UI = f(state) contract. Because the UI is a pure function of state, any data-layer bug ends up attributed to the state management layer: a button that should be disabled but is not, stale data shown after navigation, a flash of wrong content during loading. UI predictability is bounded by state-management predictability.
The Over-Engineering Trap
A common mistake is reaching for a state-management library too early.
Consider the typical evolution:
- A developer reads about Redux in a tutorial and adds it to a new project
- Every piece of state goes into the global store, including form input values and modal visibility
- The codebase accumulates boilerplate: actions, reducers, selectors, middleware
- Performance degrades because store subscriptions are coarse-grained: without selectors, unrelated field changes trigger component re-renders
- The team concludes "state management is hard" when the real problem was unnecessary indirection
The pragmatic approach:
- Use
useState/ref()for component-local state; most state is already component-scoped - Use a server-cache library (TanStack Query, SWR) for async data: these libraries handle refetch, invalidation, and optimistic updates that
useStatecan't - Only reach for a lightweight store (Zustand, Jotai, Pinia) when client state is genuinely shared across components
- Reserve state machines (XState) for flows whose valid transitions can be enumerated
This layered approach means most components have zero dependency on external state libraries, making them easier to test, move, and refactor.
Visual
State Taxonomy
Category Roadmap (FEE-601 through FEE-607)
Related FEEs
- FEE-500 Component Architecture & Design Patterns Overview -- state lives inside components; architecture determines where state belongs
- FEE-502 Reactive State & Signals -- the underlying mechanism that makes state changes visible to the UI
References
- React: Managing State -- official guide covering useState, useReducer, context, and state structure
- Vue.js: State Management -- official guide on reactive state, composables, and Pinia
- Pinia Introduction -- official docs for Vue's recommended state management library
- TanStack Query Overview -- the leading server-state library for React
- XState Documentation -- state machines and statecharts for complex UI flows
- TC39 Signals Proposal -- emerging standard for reactive state primitives
- Patterns.dev: State Management -- visual explanations of common state patterns
- Kent C. Dodds: Application State Management with React -- influential article on keeping state co-located