Frontend

What is State Management?

How a frontend application stores, updates, and shares the data that drives its UI, from component-local state to shared stores and server caches.

Definition

State is any data that can change while an application runs and that the UI depends on: form input, the open/closed status of a menu, the logged-in user, a list fetched from an API. State management is the set of decisions about where each piece of state lives, who is allowed to change it, and how components are notified when it changes. In component frameworks the UI is a function of state, so getting this right determines whether screens stay consistent or drift out of sync. Most apps mix several kinds of state. Local state (React's useState, a Vue ref) belongs to one component. Shared client state, such as a shopping cart or theme, is lifted to a common parent, passed through context, or kept in a store such as Redux Toolkit, Zustand, Jotai, MobX, or Pinia (Vue's official store, which superseded Vuex). Server state is a cached copy of data owned by a backend; libraries like TanStack Query, SWR, and Apollo Client handle fetching, caching, deduplication, background refetching, and invalidation. URL state (route params, query strings) is often the right home for filters and pagination because it survives reloads and can be shared. The main failure modes are duplicated state that falls out of sync, unnecessary re-renders when a large store changes, and hand-written caching logic that mishandles staleness. Choosing the narrowest scope that works, and treating server data as a cache rather than as client-owned state, avoids most of them.

When to Use State Management

Start with local component state and only lift it when two or more components genuinely need the same value. Reach for a global store when many distant components read and write the same client-side data, or when you need features like devtools time-travel, middleware, or persistence. Use a server-state library rather than a global store for anything fetched from an API; it handles caching and refetching you would otherwise rewrite badly. Put filters, tabs, and pagination in the URL when users should be able to bookmark or share them. Avoid a global store for form fields or transient UI flags: it adds boilerplate and makes every keystroke a global update. The trade-off is always scope versus convenience: wider state is easier to reach but harder to reason about.

State Management in Practice

  • Server state with TanStack Query: the cache key ties the data to the user ID, so switching users refetches and repeat visits are served from cache. const { data, isPending } = useQuery({ queryKey: ['user', userId], queryFn: () => fetch(`/api/users/${userId}`).then(r => r.json()), });
  • A small Zustand store for a cart that many components read; components that select only items.length re-render only when the count changes. const useCart = create((set) => ({ items: [], add: (item) => set((s) => ({ items: [...s.items, item] })), }));
  • A product listing keeps sort order and page number in the query string (?sort=price&page=3) so a reload or shared link restores the exact view without any store.
  • An e-commerce app copies the API's product list into Redux, then a price update on the server never appears because nothing invalidates the copy; moving it to a query cache with a staleTime fixes it.

Common Mistakes

  • Storing API responses in a global client store and writing manual loading/error/refetch logic. Use a server-state library that already handles caching, staleness, and deduplication.
  • Keeping derived values (filtered lists, totals) as separate state that must be updated in sync. Compute them from the source state during render or with a memoized selector.
  • Putting a large, frequently changing object in one React context, causing every consumer to re-render on any change. Split contexts or use a store with selector-based subscriptions.
  • Mutating state objects in place, so frameworks relying on reference equality never notice the change. Return new objects or use a helper like Immer that does it for you.
  • Making everything global by default. Keep state as close as possible to where it is used and lift it only when a real sharing need appears.

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.