Home > Web Front-end > JS Tutorial > ngRx Store in Angular

ngRx Store in Angular

DDD
Release: 2024-09-19 02:18:38
Original
549 people have browsed it

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 Store in Angular

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
ngRx Store 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.

ngRx Store in Angular

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

ngRx Store in Angular

Step 4: Creating selector

ngRx Store 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.

ngRx Store in Angular

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 ;)

The above is the detailed content of ngRx Store in Angular. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template