🛠 State Management in Angular Using NgRx
Tags: javascript, webdev, programming
Published: 2021-12-03 18:47:10

#### 📕 See how it's easy to manage your Angular application's state using NgRx and learn how you can use NgRx with Auth0's SDK to handle user-related functionalities.

### <br>

## Intro

State management is a key component when building applications. There are various approaches by which we can manage the state in an Angular application, each with its pros and cons.
This blog post will focus on using NgRx as our state management solution. We will look at how you can use NgRx to manage your application's state by building a Recipe Admin Dashboard application. We will also learn how to secure the application using Auth0 and how it works with NgRx.

## What Is Ngrx

[NgRx](https://ngrx.io/) is a framework for building reactive applications in Angular. NgRx is inspired by the Redux pattern - unifying the events in your application and deriving state using RxJS. At a high level, NgRx stores a single state and uses actions to express state changes. NgRx excels in managing complex states, making it ideal for applications with a lot of user interactions and multiple data sources.

## How Does Ngrx Work

NgRx is made up of 5 main components - Store, Actions, Reducers, Selectors, and Effects.

NgRx uses the Redux concept of unidirectional data flow, where all application data goes through the same lifecycle. This unidirectional data flow makes the application's state more predictable and thus easier to understand. This flow only applies to the state management layer and is not to be confused with the unidirectional data flow of the presentation layer. The following diagram shows the state management lifecycle in NgRx.

![NgRx State Management Lifecycle Diagram](https://images.ctfassets.net/23aumh6u8s0i/2DcycUsy6WR9VwxEDeiEg9/6ab8d4a7cd023248d80d2289d4e063eb/01_ngrx-how-it-works.png)

### Store

You can think of this as a client-side database. The Store in NgRx acts as the application's single source of truth. It reflects the current state of the app.

### Actions

Actions express unique events that happen in our application. These events range from application lifecycle events, user interactions, to network requests. Actions are how the application communicates with NgRx to tell it what to do.

### Reducers

Reducers are responsible for handling transitions between states. Reducers react to the Actions dispatched and executes a pure function to update the Store. Pure functions are functions that are predictable and have no side effects. Given the same set of inputs, a pure function will always return the same set of outputs.

### Selectors

Selectors are pure functions for getting slices of the state from the Store. Selectors are how our application can listen to state changes.

### Effects

Effects handle the side effects of each Action. These side effects range from communicating with an external API via HTTP when a certain Action is dispatched to dispatching another Action to update another part of the State.

## Prerequisites

Angular requires an [active LTS or maintenance LTS](https://nodejs.org/about/releases) version of Node.js. Angular applications also depend on [npm packages](https://docs.npmjs.com/getting-started/what-is-npm) for many features and functions. To download and install npm packages, you need an npm package manager such as [npm](https://docs.npmjs.com/cli/install) or [yarn](https://yarnpkg.com/).

This project has a server-side component to it that has to run in parallel when running the Frontend. Follow the instructions in the [Api Express Typescript Menu repo](https://github.com/auth0-cookbook/api_express_typescript_menu). You can read more about setting up the server-side with Auth0 in this [blog post](https://auth0.com/blog/node-js-and-typescript-tutorial-secure-an-express-api/).

This tutorial focuses on how to use Auth0 with NgRx. For more information on setting up Auth0 for Angular applications, follow the instructions on the [README](https://github.com/auth0-developer-hub/spa_angular_typescript_dashboard/blob/master/README.md#register-a-client-application-with-auth0) or refer to this [blog post](https://auth0.com/blog/complete-guide-to-angular-user-authentication/#Connect-Angular-with-Auth0).

## Getting Started Quickly

I created a demo application with the basic structure and components to help you implement the NgRx related part.

Clone the demo app and check out its starter branch:

```bash
git clone -b starter git@github.com:auth0-developer-hub/spa_angular_typescript_dashboard.git
```

Once you clone the repo, make `spa_angular_typescript_dashboard` your current directory:

```bash
cd spa_angular_typescript_dashboard
```

Install the project's dependencies:

```bash
npm i
```

Run the project locally:

```bash
npm run start
```

## Devtools

You can use the [redux devtools](https://github.com/zalmoxisus/redux-devtools-extension/) extension for Chrome or Firefox for debugging store-related operations.

To use this extension with NgRx, you'll need to add NgRx's devtools dependency to our project. You can do this using either `npm` or Angular's CLI.

Using `npm`

```bash
npm install @ngrx/store-devtools --save
```

Using Angular's CLI

> To use the Angular CLI, you will need to have the CLI installed globally. Refer to instructions on the [Angular docs](https://angular.io/cli) for how to set this up.

```bash
ng add @ngrx/store-devtools@latest
```

Import the `StoreDevToolsModule` in our `AppModule` and configure it based on your project's requirements. For this tutorial, you'll be using their default configuration. Open `app.module.ts` and add the following code 👇

```typescript
// src/app/app.module.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { AuthHttpInterceptor, AuthModule } from "@auth0/auth0-angular";

// ✨ New 👇
import { StoreDevtoolsModule } from "@ngrx/store-devtools";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { NavBarModule } from "./shared";
import { environment } from "src/environments/environment";
import { reducers, metaReducers } from "./core/state";
import { UserEffects } from "./core/state/user";
import { MenusEffects } from "./core/state/menus";

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    AuthModule.forRoot({
      ...environment.auth,
      httpInterceptor: {
        allowedList: [
          `${environment.serverUrl}/api/menu/items`,
          `${environment.serverUrl}/api/menu/items/*`,
        ],
      },
    }),
    AppRoutingModule,
    NavBarModule,

    // ✨ New 👇
    StoreDevtoolsModule.instrument({
      maxAge: 25,
      logOnly: environment.production,
    }),
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthHttpInterceptor,
      multi: true,
    },
  ],
})
export class AppModule {}
```

> If you used Angular's CLI to add NgRx devtools, this part might have been updated automatically.

### Running the App and turning on Devtools

After following the steps in this section, you should see an option to activate Redux Devtools in your toolbar when you run the app. Once activated, you should see a window with an interface similar to the image below.

![Redux Devtools with NgRx](https://images.ctfassets.net/23aumh6u8s0i/72gMjqETihKrGgQwH4a02k/229599d15c7afb9f6258b28f4aec17a2/02_ngrx-redux-devtools.png)

> You can learn more about these features from their [official documentation](https://github.com/zalmoxisus/redux-devtools-extension).

## Install NgRx

You can either use `npm` or Angular's CLI to install NgRx dependencies.

Using `npm`

```bash
npm install @ngrx/store --save
```

Using Angular's CLI

```bash
ng add @ngrx/store@latest
```

You will also be using NgRx's Effects to handle your store's side effects. Let's add that dependency.

Using `npm`

```bash
npm install @ngrx/effects --save
```

Using Angular's CLI

```bash
ng add @ngrx/effects@latest
```

> At the time this post was written, the latest NgRx store and effects version was `12.2.0`, which will be the version we will be using throughout the tutorial.

## Architecture

At its core, the app is an admin dashboard where the logged-in user can add, edit, and delete a menu item. The logged-in user will perform all the operations or a subset of it depending on their permissions. You will use NgRx to manage the state updates and user/server related events.

You will create two Stores for our application:

- _menus_: to manage menu related functionalities (CRUD operations)
- _user_: to manage user-related functionalities such as authentication using Auth0

You will also split each part of the Store into individual files to make it easier to follow throughout the tutorial.

Let's first start by creating the following folder structure in our `core` directory (you will be updating each file as we discuss each section):

```
|- src/app/core
    |- state
        |- menus
            |- menus.actions.ts
            |- menus.effects.ts
            |- menus.reducer.ts
            |- menus.selector.ts
            |- menus.state.ts
            |- index.ts
        |- user
            |- user.actions.ts
            |- user.effects.ts
            |- user.reducer.ts
            |- user.selector.ts
            |- user.state.ts
            |- index.ts
        |- core.state.ts
        |- core.reducer.ts
        |- index.ts
```

> The starter app is using a `BehaviorSubject` in `menu-state.service.ts` to manage it's state. This tutorial will walk you through migrating that to NgRx.

## Menus State Management

### Create Menus State

Let's start with creating the state object for menus. Open `menus.state.ts` and add the following code 👇
[Read more...](https://auth0.com/blog/state-management-in-angular-with-ngrx-1/?utm_source=content_synd&utm_medium=sc&utm_campaign=angular
)