NGXS Store
repository·master·Indexed 25 days ago
https://github.com/ngxs/storeA state management library for Angular designed to reduce boilerplate and simplify developer experience through a pattern-based approach. It provides tools for defining actions, managing state containers via the @State decorator, and retrieving state using Observables, Angular Signals, or snapshots. The library includes support for memoized selectors, dynamic selectors, and a separate NGXS Labs organization for experimental extensions.
What's inside NGXS
- NGXS is a state management pattern and library designed specifically for Angular applications. It provides a structured way to manage application state using a pattern inspired by Redux but optimized for the Angular ecosystem.
Overview of NGXS State Management
masterNGXS is a state management pattern and library designed for Angular applications. It serves as a single source of truth for application state and uses the CQRS (Command Query Responsibility Segregation) pattern to ensure predictable state mutations. Unlike Redux or NgRx, NGXS leverages modern TypeScript features like classes and decorators to reduce boilerplate code.Use the @ngxs/router-plugin
masterThe@ngxs/router-pluginis a plugin for NGXS that integrates the Angular Router with the NGXS store. It allows you to synchronize router state with your NGXS state tree, enabling you to react to navigation events and manage routing information within your global state.Understand the NGXS Action Life Cycle
masterNGXS treats actions as a finite state machine. Every action moves through one of four states:
DISPATCHED,SUCCESSFUL,ERRORED, orCANCELED.When you call
store.dispatch(new MyAction()), NGXS emits anActionContextwith astatusofDISPATCHED. Once the action handlers complete successfully, a newActionContextis emitted with the statusSUCCESSFUL. This triggers the observable returned by thedispatchmethod. If a handler throws an error, the status becomesERRORED.Understand the rationale for NGXS Action dispatching
masterNGXS uses Action classes for dispatching rather than direct method calls on State classes to maintain several core capabilities:
- Global State Integrity: Dispatching actions allows NGXS to manage the global state and slice specific portions for methods without losing the connection to the central store.
- Debugging and Tooling: Action dispatching enables rich debugging capabilities and hot reload support.
- Action Sharing: Using actions allows you to easily share logic and triggers between different states and higher-order states.
Use @ngxs/form-plugin for Angular form management
master@ngxs/form-plugin is a plugin for NGXS that provides specialized tools for managing Angular forms within your NGXS state. It allows you to sync form state with your NGXS store, making it easier to handle complex form structures and validation states.Explore NGXS Labs
masterNGXS Labs is a collection of community-driven libraries that augment the main framework. These libraries are experimental and evolve independently from the core
@ngxs/storelibrary.Notable Labs packages include:
@ngxs-labs/entity-state: For managing entity-based state.@ngxs-labs/testing: Testing utilities.@ngxs-labs/immer-adapter: Adapter for using Immer with NGXS.@ngxs-labs/dispatch-decorator: Decorator for dispatching actions.@ngxs-labs/select-snapshot: Decorator for selecting state snapshots.
Understand the core NGXS concepts
masterNGXS is built around four major concepts that create a circular control flow:
- Store: Acts as the global state container, the action dispatcher, and the selector provider.
- Actions: Classes that describe a specific action to be taken along with any associated metadata.
- State: Class definitions that represent specific slices of the application state.
- Selects: Selectors used to retrieve specific slices of state.
Control Flow Pattern: A component dispatches an Action $\rightarrow$ the Store reacts to the action $\rightarrow$ the component receives updated data via a State Select.
Understand NGXS Labs
masterNGXS Labs is a separate GitHub organization used to host experimental libraries and community-driven extensions that augment the core@ngxs/storeframework. This allows for rapid innovation and experimentation without compromising the stability of the main NGXS core. Once a Labs project stabilizes and gains significant adoption, it may be moved to the mainngxsorganization.Use Exposed Internals for Advanced Development
masterNGXS exposes certain internal API implementations to allow library authors to experiment with advanced extensions. These symbols are marked with aɵprefix, indicating they are not part of the official API and are subject to change without notice. Use these only if you are building advanced utilities or custom plugins that require access to internal types or configurations.Explore NGXS Community Projects
masterBrowse a collection of community-driven projects built with NGXS to see real-world implementations. These include authentication flows, Electron-based applications (terminal emulators, file managers), mobile apps using Ionic/Capacitor, and various starter kits and tutorials.Introduction to the NGXS Todo Tutorial
masterThis tutorial guides you through building a Todo application to learn the core concepts of NGXS. You will learn how to:
- Install NGXS into an Angular project using schematics.
- Use Actions to represent user interactions and data fetching.
- Use State to manage application data.
- Use Selectors to retrieve and display data within components.
The tutorial starts with a static application that you will progressively transform into a dynamic, state-managed application.