Thursday, April 5, 2018

NgRx selectors

In a Redux selector in an Angular application one hands in at least one function that returns a stateful object and then gets back a function that returns an object like so:

import { DataContext, MyStateModel } from '../data-models';
import { createSelector } from '@ngrx/store';
 
export const getStuff = function (state: DataContext) {
   return state && state.myState ? state.myState : new MyStateModel();
};
 
export const getSpecificStuff = createSelector(
   getStuff,
   (x: MyStateModel) => {
      return x.mySpecificState;
   }
);

 
 

The last argument defines the shape of what should be handed back. The preceding argument in the example above is a function returning state, but it should be noted that there could be n number of these handed in before the definition for what should be returned including other selectors. You may hand selectors in as the leading arguments into other selectors and in this manner drill down to the data you want. Note that if there were two upfront arguments there would also be two variables in the signature of the last setting as well.

No comments:

Post a Comment