↓ Skip to main content
Life

NGRX

·2 mins

Inspired by Redux, Store is RxJS powered state management for Angular applications. Store is a container for state of the application.

Refer to documentation at – https://ngrx.io/guide/store

Key components
#

I have a sample application posted at https://www.github.com

Actions
#

Unique events generated from components and services.

#v8 Onwards
export const login = createAction('[Login Initiatied] Login',
  props<{ principal: IPrincipal }>()
)
#Older way of creating actions is still valid.
import { Action } from '@ngrx/store';

export class Login implements Action {
  readonly type = '[Login Page] Login';
  constructor(public principal: IPrincipal) {}
}

You can dispatch an action like below

import * as appActions from '../actions/login.action';
principal = {
      email: this.username.value,
      password: this.password.value
    };

this.store.dispatch(appActions.login({ principal }));

Example Actions
#

Action-Reducer-State
export const login = createAction('[Login Initiatied] Login',
  props<{ principal: IPrincipal }>()
);

export const loginSuccess = createAction('[Login Success] Login',
  props<{ userProfile: IUserProfile }>()
);

export const loginFailure = createAction('[Login Failure] Login',
  props<{ message: any }>()
);

export const logout = createAction('[Logout initiated] logout', 
props<{ userName: string}>()
);

Reducers
#

Pure functions that manipulate the state.

const loginReducer =
  createReducer(
    null,
    on(LoginActions.login, (state, { principal }) => {
      return {
          loggedIn: false,
          userProfile: {
            user: principal,
            details: null,
            profile: null
          }
      };
    }),
.
.
.

Selectors
#

Selectors are pure functions to select, derive, compose pieces of state.

export const selectTheState = createSelector(selectState, state => state);

Flow of state

Flow of state

Example
#

Installation
#

In my sample application my pacakge.json looks something like this for NGRX specific libraries

"@ngrx/effects": "^8.2.0",
"@ngrx/router-store": "^8.2.0",
"@ngrx/store": "^8.2.0",
"@ngrx/store-devtools": "^8.2.0",

One has to first install the basic store

npm install @ngrx/store --save
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Security Assertion Markup Language

·1 min
What is SAML? # Security Assertion Markup Language (SAML) is an open standard that allows identity providers (IdP) to pass authorization credentials to service providers (SP). It implements a secure method of passing user authentications and authorizations (users, logins, and attributes) between the identity provider and service providers.

CSS continued

·1 min
Pseudo classes # State dependent styling, e.g Style a checkbox when it is in checked state. It is usually denoted by a preceding colon ‘:’ (without the quote).

How much more can you take?

·4 mins
Most of the times it is my wife who is diligently taking care of my little devil and sometimes I tag along. She is the one who has taken up the mettle of enduring the mud and crap the society throws at her. She is the one who has even given me courage everyday. One such face-offs she had quiet recently was when she was out with my little one, on one of the usual evening outings. My daughter is restless, full of energy and obviously autistic. Her biggest shortcoming she is unable to learn the semantics of – what you call a normal conversation.