Skip to content

๐Ÿง  State Management

Every piece of state has a cost. A Programmer distinguishes between Global State (shared across the app), Local State (contained in one component), and Server Cache (mirrored data from an API). The best state management strategy is often the one that stores the least amount of data globally.


๐Ÿ›๏ธ 1. Unidirectional Data Flow (Redux, Vuex)

Section titled โ€œ๐Ÿ›๏ธ 1. Unidirectional Data Flow (Redux, Vuex)โ€

The โ€œStrictโ€ architecture. Inspired by the Flux pattern, it ensures that state changes are explicit and traceable.

  • How it works: Action โฎ• Dispatcher โฎ• Store โฎ• View. You never mutate state directly; you dispatch an action that a Reducer uses to calculate a new state.
  • Pros: Predictable, excellent debugging (Time-travel debugging), and easy to test business logic.
  • Cons: High boilerplate (โ€œCeremonyโ€). You often need many files just to update a single boolean.

The modern evolution. These libraries strip away the boilerplate while keeping the benefits of a central store.

  • Zustand (React): A small, fast, and scalable state management solution using hooks. It doesnโ€™t require a โ€œProviderโ€ wrapping your app, which avoids unnecessary re-renders.
  • Pinia (Vue): The successor to Vuex. It is intuitive, supports Hot Module Replacement, and has native TypeScript support.
  • Pros: Extremely low boilerplate, high developer velocity, and easy to split into โ€œstoresโ€ (e.g., useUserStore, useCartStore).

The current โ€œfrontierโ€ of state management (Solid.js, Preact, Angular, Vue).

  • How it works: Instead of a component re-rendering because an object changed, a Signal is a wrapper around a value. When the value changes, the Signal surgically updates only the specific DOM nodes or computations that โ€œsubscribedโ€ to it.
  • Pros: Performance is incredible. It eliminates the โ€œVirtual DOM Diffingโ€ overhead for state updates.
  • Cons: Requires a different mental model (you are dealing with โ€œreferencesโ€ to values rather than the values themselves).

PatternExamplesBest ForComplexity
Flux / ReduxRedux Toolkit, VuexMassive teams, highly complex state transitions.High
Hooks / StoresZustand, PiniaMost modern web apps, rapid development.Low
AtomsRecoil, JotaiApps with complex, cross-dependent state pieces.Moderate
SignalsSvelte, Solid, AngularPerformance-critical apps, high-frequency updates.Low/Moderate

1. The โ€œServer Stateโ€ vs. โ€œUI Stateโ€ Split

Section titled โ€œ1. The โ€œServer Stateโ€ vs. โ€œUI Stateโ€ Splitโ€

A common Programmer mistake is putting API data in Redux/Zustand.

  • Programmerโ€™s Move: Use React Query (TanStack Query) or SWR for server data. These handle caching, revalidation, and loading states automatically. Use your state manager (Zustand/Pinia) only for true UI state (e.g., โ€œIs the sidebar open?โ€, โ€œCurrent themeโ€).

Avoid nested objects in your store. If you have a list of posts, each with an array of comments, itโ€™s hard to update.

  • Strategy: Store them like a database: posts: { [id]: post } and comments: { [id]: comment }. Use IDs to link them. This makes updates $O(1)$ and prevents data inconsistency.

Never store data in the state that can be calculated from other data.

  • Example: Donโ€™t store totalPrice. Store the items array and use a Selector (like reselect or a computed property) to calculate the total. This ensures the total is always โ€œin syncโ€ and avoids โ€œdual source of truthโ€ bugs.

๐Ÿ’ก Note: Before reaching for a library, ask: โ€œCan I solve this with a simple Prop Drill or the Context API?โ€ For many apps, the built-in tools are sufficient. Only introduce a state management library when the complexity of passing data between distant components becomes a bottleneck for development.