Using Redux in Angular Application for State Management

Posted By :Shiv Shankar Shah |29th June 2020

using Redux in Angular application

 

 

Redux is a state container for angular apps. It was developed by Dan Abramov and Andrew Clark in 2015. Redux is mainly used for managing a complex, changing state in complex modern single-page applications. Redux library provides a method called createStore() which takes reducer as an argument by which state of the store is going to be modified. This blog post serves as a guide to using Redux in Angular application for state management.

 

e.g: const store=Redux.createStore(reducer);

 

Under AI Development, Redux helps developers in understanding how the data flow works in an application. Also, the reducer function simplifies the testing of logic that leads to faster and better application deployment.

 

 

The components of Redux architecture are explained below:

 

1. Store:  A store acts as a database where the entire state of your application is stored. It manages the status of the application and has a dispatch i.e action function. 

 

2. Action: Action is dispatched from the view which are payloads that are read by Reducers. It is a pure object created to store information about the user's event. 

E.g: store.dispatch({type: "LOGGED_IN_STATE", payload: {user:email, password}})

 

3. Reducer: Reducer reads the payloads from the actions and updates the store values. It must not mutate the current state directly and not use any data outside of its arguments. For reducer, the current state is read-only. It does not change the state instead, it returns a new state.

 

Redux follows thee principles. They are:

 

1. Single source of truth

=> In redux, there is only one store object i.e called application state, for the entire application which resides in the store.

 

2. State is read-only

=> In redux, state is read-only. The only way to change the state is by dispatching an action in redux.

 

3. Changes are made with pure functions/reducers

=> For changes, we have to use reducers. Reducers are pure functions that take the previous state and an action and return the new state. Here, dispatched actions will be received by the reducer which modifies the state in the store if required.

 

Installation Steps:

 

=> npm install --save redux @angular-redux/store

 

=> npm install --save redux-logger

 

=> npm install --save-dev @types/redux-logger

 

We can install redux in our project by running above commands in the terminal. Use the  @select decorator to access your store state, and .dispatch() to dispatch actions.

 

The Action interface:

interface Action {

  type: string;

  payload?: any;

}

 

The reducer interface:

interface Reducer<T> {

  (state: T, action: Action): T;

}

 

Creating Reducer: 

let reducer: Reducer<number> = (state: number, action: Action) => {

  return state;

};


 

We, at Oodles, as an emerging Machine Learning Development Company, are a team of AI Software Architects, developers, and analysts. We employ effective tools like Redux, React, and more to build dynamic Angular applications. 

 

Join forces with our AI Development team to build and deploy resilient AI and machine learning applications for your enterprise.

 

 


About Author

Shiv Shankar Shah

Shiv is always eager to learn and believes in planning. Proficient in Java, Selenium, Angular 7+ etc. He is grateful for every precious moment life offers him, it allows him to see the miracle in each experience. He is fond of playing Chess.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us