DEV Community

Cover image for State management patterns
John Tsourtis
John Tsourtis

Posted on

State management patterns

These days, there is a lot of discussion around new state management tools that you can use in your React projects, but there isn't a lot about the actual philosophy behind them.

So today, we are going to talk about the patterns used behind these tools.


What is exactly the Flux pattern that Redux and Zustand are based on?

Flux pattern image

The Flux pattern is an architectural design pattern introduced by Facebook for building client-side web applications. It is the conceptual foundation for libraries like Redux and Zustand. Here's a breakdown of its key components and principles:

  1. Unidirectional Data Flow: The core principle of Flux is that data should flow in a single direction. This makes the logic of your application more predictable and easier to understand.

  2. Components: These are the building blocks of your application's UI. In a Flux architecture, components listen to stores and re-render when data changes.

  3. Actions: Actions are simple objects containing a type and some data. They are the only source of information for the store and are sent to the dispatcher to trigger updates to the store.

  4. Dispatcher: This is a central hub that manages all data flow in a Flux application. It receives actions and dispatches them to the stores.

  5. Stores: Stores contain the application's state and logic. They are similar to a model in a traditional MVC (Model-View-Controller) setup but manage the state of a domain within the application. Stores register with the dispatcher and update their state in response to actions.

Redux and Zustand, while inspired by Flux, introduce their own flavors:

  • Redux is a predictable state container for JavaScript apps, often used with React. It maintains the unidirectional data flow of Flux but simplifies the dispatcher concept. In Redux, there's typically a single store with reducer functions handling actions to update the state.

  • Zustand is a smaller, simpler state management solution. It provides a straightforward and minimal API to create stores. Zustand eliminates much of the boilerplate associated with Redux, offering a more direct and less verbose way to manage state.

In summary, Redux offers a more structured approach with a single store and clear separation of concerns, whereas Zustand is more flexible and minimalistic.


What is exactly the Atomic State pattern that Recoil and Jotai are based on?

Atomic State pattern

The Atomic State pattern, as used in state management libraries like Recoil and Jotai, represents a different approach compared to traditional Redux-like state management. This pattern focuses on the idea of "atoms" as the fundamental units of state. Here's a breakdown of its key concepts:

  1. Atoms: At the core of the Atomic State pattern are atoms. An atom is a unit of state. In the context of Recoil or Jotai, atoms are small pieces of state that can be read from and written to from anywhere in your application. They are independent and can be combined to form more complex state structures.

  2. Selectors: Selectors are pure functions that take atoms or other selectors as input and return some derived state. This allows you to create dynamic data that depends on other atoms. Selectors can compute derived data based on atoms, and they re-evaluate when the atoms they depend on change.

  3. Unidirectional Data Flow: Similar to Flux, the Atomic State pattern encourages unidirectional data flow. However, it differs in how it handles this flow. In Atomic State, the focus is on individual atoms and how they update and influence the application state.

  4. Component Subscription: Components can subscribe to atoms and selectors. When an atom or selector that a component is subscribed to changes, the component re-renders with the new state. This makes it easy to manage local and shared state across your application.

  5. Concurrency and Asynchronous Operations: Libraries like Recoil provide mechanisms to handle asynchronous operations and concurrency within your state management. This includes features for handling data fetching, suspense, and more.

Differences between Recoil and Jotai:

  • Recoil: Developed by Facebook, Recoil provides a robust framework for state management in React applications. It introduces concepts like atom families for managing related atoms and integrates well with other React features like Suspense.

  • Jotai: Jotai is a smaller, more minimalistic library compared to Recoil. It aims for simplicity and a smaller API surface. It's often preferred for its straightforward approach and ease of use.

In summary, the Atomic State pattern, as implemented in Recoil and Jotai, focuses on fine-grained control of state using atoms and selectors.


Which architectural pattern does Valtio and Mobx use in their implementations?

Proxy State pattern

Valtio and MobX use the Proxy State pattern for state management in JavaScript applications, particularly with React. This pattern is based on JavaScript's Proxy object, enabling reactive state management that's both flexible and intuitive. Here's how each of these libraries applies the Proxy State pattern:

Valtio

  • Use of Proxies: Valtio leverages JavaScript proxies to create state objects. These proxies automatically track changes to their properties, making the state inherently reactive.
  • Simplified State Management: Valtio simplifies state management by allowing direct mutations on the state object while still triggering re-renders in React components.
  • Reactivity: Any changes to the state are automatically propagated to the components that use them, encouraging a more intuitive and straightforward approach to state updates.

MobX

  • Observables: MobX uses the concept of observables, which are essentially JavaScript objects, arrays, or primitive values turned into reactive data through proxies.
  • Actions and Reactions: Changes to observables are usually made through actions, and reactions are used to automatically track these changes and update the UI or other parts of the application accordingly.
  • Fine-grained Reactivity: MobX offers a more fine-grained reactivity system, where changes to specific properties can trigger specific reactions, allowing for optimized performance and more controlled state management.

Common Features

  1. Reactivity: Both Valtio and MobX provide a reactive system where changes in the state are automatically detected and used to update the UI.

  2. Simplified Data Flow: Unlike patterns like Redux or traditional Flux, where there's an emphasis on unidirectional data flow and explicit actions, the Proxy State pattern allows for more direct manipulation of state with less boilerplate.

  3. Ease of Use: Both libraries aim to simplify state management, making it more accessible and less verbose compared to more traditional state management solutions.

In summary, Valtio and MobX use the Proxy State pattern, leveraging JavaScript proxies to create reactive and intuitive state management systems. This pattern stands out for its direct and less boilerplate-heavy approach compared to other patterns like Redux's Flux architecture.

Top comments (0)