Redux Implementation in Angular 2

Redux Introduction

Redux is a javascript library to store and manage application state in single state tree and share it across components or objects in a very efficient way. Most of javascript developers use redux in ReactJS application, but actually redux is an independent library so we can use it with any others javascript framework.

But why redux ? what problems are redux solving ? Let’s imagine that in a highly asynchronous environment we have several buttons (let say over than 20) in our page, each one of them leads to a change of 5 or 6 properties in our app, how do we know which states we are in ? we could lost in the middle of nowhere and have no idea what bugs will we find. Redux helps us to predict the next state and remember the previous states (a.k.a time machine).

Redux in Angular 2

Angular 2 armed with a powerful procedure to handle data flow between parent and child component by declaring @input decorator within child component to receive data from parent and declaring @output as event emitter in child component for sending data to parent.

In the case above we see that our parent component has one child component, we can figure the communication between components as below:



Angular 2 Component Interaction

For small kind of application which has a very little of interaction between components there wouldn’t be a problem, but when the application getting bigger and involved so many interactions, managing the state component per component as showing above will be very hard.

Now let’s go to the step-by-step how to use redux within our angular application:

  1. Install redux from npm
  2. Create actions
  3. Create reducers
  4. Create Store
  5. In parent component, subscribe to listen on every state changes
  6. Update state in child component (no more event emitter)

Our angular code above will given the following result:

Now we are using redux and it’s change the way our component communicate each other:

Angular 2 + Redux Component Interaction


Redux offers really great features in order to develop such a complex web application, with a single source of truth (a.k.a store) it would be easier to maintain the application state. It also offers a clean way to map properties in the data store with properties declared in application’s model every time the store changes state. One last thing, with the mighty time-machine, some functionality which has been traditionally difficult to implement – Undo/Redo, for example – can suddenly become trivial to implement.

External resources:

Leave a Reply

Your email address will not be published. Required fields are marked *