Building a Redux Application Using Angular

Posted By : Krishna Jangir | 22-Jun-2020

State management has been an ongoing issue in front-end frameworks. Front-end frameworks' key tenet of state mutation at multiple levels makes the standard MVC (Model-View-Controller) approach ineffective. This multi-layered state mutation was evident in Angular 1, where the logic for managing the application state was distributed between directives, controllers, and service, and each level had its own logic for mutating the state. The segmented application state became prone to causing inconsistencies and was difficult to test.

 

Such issues have become more apparent and difficult to circumvent as front-end applications started to become increasigly complex and more reactive to user input. However, increasingly popular traits of AngularJS application development, such as an emphasis on functional programming, have given birth to a novel state management model called Redux. Redux centralizes the state into a single entity, granting developers access to the most recent state anywhere in the application.

 

Even though Redux arose through the React community, third-party libraries such as ngrx/store and extensions such as rxJS, have made Redux an equally suitable concept for AngularJS app development services.

 

In this guide, I will cover the core concepts of Redux and how they boost Angular 2 applications.

 

Main Concepts of Redux

Redux comprises three main parts

  • The main store
  • Reducers
  • Actions

Each of these parts plays a different role in the mutation of the application's state. Middlewares, which are used to handle asynchronous requests (such as API calls), will be covered in part two.

 

The Main Store

The store combines the whole application state into a single entity, acting as the database for the web application. The store is broken down into different states, which represent different types of data in the application. The state is immutable, but it can be altered by explicitly defined actions. Thus, state mutation is restricted and centralized, simplifying the debugging process and making code more understandable.

 

Reducers

If the store is the database of the application, the reducers are the tables. Reducers represent slices, or structures in the application that are composed in a particular fashion. A reducer is a pure function that defines how a slice of the state is going to change when an action is being dispatched. It accepts two arguments, the previous state and an action, and returns the new state.

export interface Reducer<T> {
  (state: T, action: Action): T;
}
Example

export const itemsReducer: ActionReducer<number> = (state = [], action: Action) => {
  switch (action.type) {
    case ADD_ITEM: //adding an item
      const item:Item = action.payload;
      //KEEPING THE STATE IMMUTABLE:
      // We don't perform actions that alter the state such as
      // array.push, array.shift and so on.
      // Instead, we concatenate the item, preserving the state.
      return [ ...state, item ];
    }

    case REMOVE_ITEM://removing the item
    // Again, we don't remove the item, we just filter the new state so
    // that it won't contain the item we're removing
      return state.filter(item => {
        return item.id !== action.payload.id;
      });
    }
}

Actions

Each reducer has a set of action types that define how the state should be changed. An action is composed a type and a payload:

export interface Action {
 type: string;
 payload?: any;
}

//making an action for adding an item
dispatch({type: ADD_ITEM, payload: {id: 1, name: 'An item' , category: 'miscellaneous'}})

Overview

When an action is dispatched, the reducer takes it and applies the payload depending on the action type, and outputs the new state.

description

The store encompasses the whole state, the reducers return fragments of the state, and actions are pre-defined, user-triggered events that communicate how a given fragment of the state should change. Middlewares are used in cases the actions require asynchronous requests.

 

Projecting Data

As you already know, the Store is a tree-like structure representing the application state, that is composed of reducers, which represent different slices of the state. In the case of Angular 2's ngrx/store, the store is observable, hence making the access to the application's state reactive. An observable store also allows us to mix the values of several states using RxJS's operators.

//getting a single slice of the state
store.select('items')

//combining multiple slices
Observable.combineLatest(
  store.select('items'),
  store.select('categories'),
  (items, categories) => {

})

Redux in practice

Now that you know the main concepts, you're probably wondering how they tie together in an Angular 2 application. To illustrate how Redux works, we are going to build a simple financial accounting tool that will keep track of your transactions using the Redux architecture. It will feature operations which will either add or deduct money from an imaginary account. Later on, we are going to add a way to see your current balance and some additional statistics.

description

Key Takeaways

  • Handling more than one state and reducer
  • Composing states and filtering
  • Structuring larger applications
  • Role and application of effects in Redux

 

At Oodles, we provide end-to-end SaaS development services with a focus on building high-quality websites and web applications using JavaScript-based technologies. Our development team is skilled at using Full stack development frameworks including AngularJS and NodeJS to build scalable and responsive web applications for clients. We have completed several Angular and MEAN stack development projects for clients from across the globe.

About Author

Author Image
Krishna Jangir

She has skills on JavaScript frameworks like ionic , angular 4+ and basic knowledge on html, css. She is fond of learning and explore the things.

Request for Proposal

Name is required

Comment is required

Sending message..