Abhijit
Abhijit

Reputation: 941

NGXS State Functionality

Suppose we save information and retrieve it from database ( as it might change by multiple users ), and update the state on each save and retrieve operation. What will be the use of state, when should we use it? Example: Update quantity of item in database, when app is used by multiple users.

Please explain what is exact use of state in NGXS

Upvotes: 0

Views: 586

Answers (1)

Rich Duncan
Rich Duncan

Reputation: 1923

tldr; When you retrieve information from your database (or REST api more likely) you have to manage it some how so that your components can display a 'view' of the data to the user. What I like about the CQRS pattern and NGXS in particular is the uniformity of the way state is managed and the level of decoupling between events that effect the data the places in which it is used.

Here's an example - lets say you have 10 major entities that your app manipulates and they are coming from one or more REST apis that your user as been authenticated/authorized to access. The user logs out. If you manage your state in 10 separate services, you have to handle the logout in each. In NGXS you dispatch a logout action and have all your state containers listen for that event and take action accordingly.

Representing state with Observables means you can change your data in one place (an action) and now that the right thing will happen in the component tree. Say you're displaying a list of items, a selected item in the list and a selected sub item. If an action updates the state representing the list, all of the components observing it automatically update them selves.

Upvotes: 2

Related Questions