Angular 6 NGXS example – Angular State Management

NGXS Store is a state management solution for Angular apps that helps us build applications by working around our app’s data (state). In this tutorial, we’re gonna look at how to work with NGXS Store, custom Actions, handle dispatched Actions with @Action and @Select/@Selector decorator. Then we will practice to understand all of them in a simple practical Angular 6 example.

Related Posts:
– NgRx: Angular 6 NgRx Store example – Angular State Management
– State Management with Redux:

– Reactive Streams:

NGXS Store to manage App State

Why we need a State container

State container helps JavaScript applications to manage state.
=> Whenever we wanna read the state, look into only one single place – NGXS Store.
=> Managing the state could be simplified by dealing with simple objects and pure functions.

NGXS Store

Store holds the current state so that we can see it as a single source of truth for our data.
– access state using store.select(property) or @Select/@Selector decorator.
– update state via store.dispatch(action).

@Select() decorator is the way to slice data from the store. We can use it comfortably:

Action

Action is payload of information that is sent to Store using store.dispatch(action).
Action must have a type property that should typically be defined as string constants. It indicates the type of action being performed:

Handle dispatched Actions

NGXS gives us a StateContext object to mutate the store in response to an Action. We don’t need to separate this logic in a Reducer and/or Action stream.

We use getState() to get the current state, setState() to return the state.
In this example, we use patchState() to make updating the state easier: only pass it the properties we wanna update on the state and it handles the rest.

Practice

Example overview

angular-6-ngxs-store-example-project-structure

This is a simple Angular 6 NGXS Application that has:
CustomerStateModel (customer.state.ts) as the main state that is stored inside NGXS Store.
– 2 types of Action: CREATE_CUSTOMER and DELETE_CUSTOMER (customer.actions.ts).
– Dispatched Actions will be handled with @Action decorator (customer.state.ts).

We can save/remove Customer. App will update UI immediately.

angular-6-ngxs-store-example-result

>> Click on Delete button from any Customer:

angular-6-ngxs-store-example-delete-result

Step by step

Install NGXS Store

Run cmd: npm install @ngxs/store.

Create Data Model

app/customers/models/customer.ts

Create Actions

app/actions/customer.actions.ts

Create State & handle dispatched Action

app/state/customer.state.ts

Import NGXS Store

app/app.module.ts

Create Components
Create Customer Component

customers/create-customer/create-customer.component.ts

customers/create-customer/create-customer.component.html

Customer Details Component

customers/customer-details/customer-details.component.ts

customers/customer-details/customer-details.component.html

Customers List Component

customers/customers-list/customers-list.component.ts

customers/customers-list/customers-list.component.html

Import Components to App Component

app/app.component.ts

app/app.component.html

Source Code

Angular6-NGXS-example

By grokonez | August 24, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*