Manage State Across LWC Components with State Managers

State management in 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.

LWC state management will soon be available as a developer preview. State management isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. All commands, parameters, and other features are subject to change or deprecation at any time, with or without notice. Don't implement functionality developed with these commands or tools.

I agree that my participation and the participation of my organization and its Affiliates in this Developer Preview Program is subject to the Main Services Agreement for Developer Services and the Developer Terms of Use, including any applicable content, usage guides, or policies provided by Salesforce, and that any information disclosed by Salesforce as part of the Developer Preview Program is Salesforce Confidential Information. I acknowledge that Developer Preview Programs are provided AS IS, exclusive of any warranty or support, can contain bugs or errors, are provided for evaluation and testing purposes, are not intended for production use, are not necessarily made generally available, and can be discontinued at any time.

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.