Manage State Across LWC Components with State Managers (Beta)

State management for Lightning Web Components (LWC) is a forthcoming feature designed to help developers group and manage data and its related logic more effectively within their applications. A state manager provides a structured way to handle application state, making it easier to share data across components, manage complex data interactions, and separate data concerns from presentation logic.

State management for LWC is a pilot or beta service that is subject to the Beta Services Terms at Agreements - Salesforce.com or a written Unified Pilot Agreement if executed by Customer, and applicable terms in the Product Terms Directory. Use of this pilot or beta service is at the Customer's sole discretion.

As applications grow in complexity, it can be challenging to manage the flow of data, ensure components react appropriately to changes, and keep your codebase clean. LWC state management offers an idiomatic, LWC-native solution to these problems by introducing the concept of state managers.

A state manager is essentially a dedicated JavaScript module that encapsulates a related set of data (the "state") and the functions (or "actions") that operate on that data. This approach allows you to decouple your data logic from your component's rendering logic, leading to more organized, maintainable, and testable code.

A state manager implementation consists of the following elements.

  • A factory function to create and initialize the state manager.
  • A set of named properties that hold the state. These can be simple values or values derived from other state (computed values).
  • A set of named actions (functions) that can modify the state.

State managers work within the LWC Framework’s reactivity system. Changes to a state manager's exposed values automatically trigger updates in components that reference those values, including re-rendering those components when needed.