DEV Community

Cover image for ngRx Store in Angular
Aniket Lodh
Aniket Lodh

Posted on

ngRx Store in Angular

Before I start, a small backstory. So, a few weeks back, there was a requirement in an angular project I was working on, wherein I had to keep a piece of data intact so that several other components could use it. My React instinct suggested me of using some angular version of context API which led me to 3 Angular ways of managing states and keeping data intact.

  1. Component State: For Simple applications or components with minimal shared data, we can manage state within individual components using Angular's component properties and event binding. This was clearly out of league because I needed to share data across multiple components.

  2. Service Based State Management: Services can be used to create centralized state management solutions. But performance and scalability of the application will take a hit following this approach.

  3. ngRx Store: ngRx store provides centralized state management capabilities. NGRX uses RxJS observables and actions to manage state.

Out of the three ngRx Store seemed to be the most suitable one to go with.

Implementing ngRx Store:

To better understand the implementation of ngRx, we first need to understand how ngRx works in the first place.

The flow starts from the component. The good thing about ngRx is our component doesn't need to know how to manage the state. Only thing it is concerned about is dispatching an action to inform something happened (an event) like the user clicked on the delete button.

ngRx State management Lifecycle

At this point the reducer comes into play. Reducer is responsible for determining how a particular action should modify state. Usually we have reducer for every entity we want to manage state for. So, Reducer detect all the action being dispatched in the app and determine how the state should be modified and once modified, stores the updated state in the ngRx Store (which is a global store for all the states across the application).

Finally, when a component wants to access the store, the component uses a selector to pull in the state that it needs from the store.

Now let's get our (actually my) hands dirty and implement a store for a Todo application.

Step 1: Creating action
how to create Action in Angular

Inside the createAction method, we are passing two parameters - a unique string that will be used to distinguish an action, another is the payload we want to send (optional).

Step 2: Creating Reducers
In the reducer, we pass the initial state and the task to perform on dispatching a particular action.

Creating Reducer in Angular

Step 3: Dispatching Action
We dispatch an action by calling the dispatch method of the store passing the action to it.

Dispatching action in Angular

Step 4: Creating selector

Creating selector in Angular

AppState refers to the central store for the whole application from which we selecting the todo state. Now we can start consuming the store data like I did in the Dispatching Actions code's line no. 11.

app module

Finally, we need to add the todoReducer in our imports of the App Module so that it is available across the application.

Conclusion

In this blog, I summarized and explained the ngRx store in details, how it works and how can we implement it in our application. ngRx store can be very useful as the application grows, to store data and access it across different components. Hope it helped ;)

Top comments (0)