Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is ngrx createSelector and createFeatureSelector?

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 ); 
like image 784
Karty Avatar asked Oct 29 '17 10:10

Karty


People also ask

What is createSelector NgRx?

The createSelector can be used to select some data from the state based on several slices of the same state. The createSelector function can take up to 8 selector functions for more complete state selections. For example, imagine you have a selectedUser object in the state.

What are reducers in NgRx?

Reducerslink. Reducers in NgRx are responsible for handling transitions from one state to the next state in your application. Reducer functions handle these transitions by determining which actions to handle based on the type.

What is a key building block of NgRx?

Key concepts To build and operate on our state, we will need the following basic building blocks: Store is where state of the application will be stored. It's both an Observable of a state and an Observer of action. Actions describe all the possible and unique events that can occur within the application.


1 Answers

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

like image 80
dee zg Avatar answered Oct 13 '22 02:10

dee zg