Building a Reactive Application with Ngrx and Angular 2
What Is Reactive Programming?
Reactive programming is a term that you hear a lot these days, but what does it really mean?
Reactive programming is a way applications handle events and data flow in your applications. In reactive programming, you design your components and other pieces of your software in order to react to those changes instead of asking for changes. This can be a great shift.
A great tool for reactive programming, as you might know, is RxJS.
By providing observables and a lot of operators to transform incoming data, this library will help you handle events in your application. In fact, with observables, you can see event as a stream of events and not a one-time event. This allows you to combine them, for example, to create a new event to which you will listen.
Reactive programming is a shift in the way you communicate between different parts of an application. Instead of pushing data directly to the component or service that needed it, in reactive programming, it is the component or service that reacts to data changes.
What is Ngrx?
Ngrx is a group of Angular libraries for reactive extensions. Ngrx/Store implements the Redux pattern using the well-known RxJS observables of Angular 2. It provides several advantages by simplifying your application state to plain objects, enforcing unidirectional data flow, and more. The Ngrx/Effects library allows the application to communicate with the outside world by triggering side effects.
In order to understand the application, we must make a quick dive into the core Redux concepts.
The store can be seen as your client side database but, more importantly, it reflects the state of your application. You can see it as the single source of truth.
It is the only thing you alter when you follow the Redux pattern and you modify by dispatching actions to it.
Reducers are the functions that know what to do with a given action and the previous state of your app.
The reducers will take the previous state from your store and apply a pure function to it. Pure means that the function always returns the same value for the same input and that it has no side effects. From the result of that pure function, you will have a new state that will be put in your store.
Actions are the payload that contains needed information to alter your store. Basically, an action has a type and a payload that your reducer function will take to alter the state.
Dispatchers are simply an entry point for you to dispatch your action. In Ngrx, there is a dispatch method directly on the store.
Middleware are some functions that will intercept each action that is being dispatched in order to create side effects, even though you will not use them in this article. They are implemented in the Ngrx/Effect library, and there is a big chance that you will need them while building real-world applications.
When you feel like the state of your application is being updated from different sources and it starts to become a mess, Ngrx is what you need.
To develop custom web app using Angular, please visit our technology page.