what is ngrx createSelector and createFeatureSelector?

Karty picture Karty · Oct 29, 2017 · Viewed 15.1k times · Source

I have been reading the code of ngrx example app and find two function calls

  1. createFeatureSelector<AuthState>('auth');

and

  1. createSelector(selectAuthState,(state: AuthState) => state.status);

What does this do?

export const selectAuthState = createFeatureSelector<AuthState>('auth');

export const selectAuthStatusState = createSelector(
  selectAuthState,
  (state: AuthState) => state.status
);

Answer

dee zg picture dee zg · Oct 29, 2017

Its used as an optimization step for store slices selection. For example, if you return some heavy computation result for some store slice, then using createSelector will do memoization which means it will keep track of last input params to selector and if they are the same as current ones, it will return last result immediately instead of repeating computation.

ref: https://github.com/ngrx/platform/blob/master/docs/store/selectors.md