首页 > web前端 > js教程 > ngRx Store in Angular

ngRx Store in Angular

DDD
发布: 2024-09-19 02:18:38
原创
556 人浏览过

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

以上是ngRx Store in Angular的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板