What is store.select in ngrx What is store.select in ngrx angular angular

What is store.select in ngrx


In very simple terms select gives you back a slice of data from the application state wrapped into an Observable.

What it means is, select operator gets the chunk of data you need and then it converts it into an Observable object. So, what you get back is an Observable that wraps the required data. To consume the data you need to subscribe to it.

Lets see a very basic example.

  1. Lets define the model of our store

    export interface AppStore {   clock: Date}
  2. Import the Store into your component from '@ngrx/store'

  3. Create a store by injecting into the constructor

    constructor(private _store: Store<AppStore>){}
  4. Select returns an Observable.

    So, declare the clock variable in your component as follows:-

    public clock: Observable<Date>;

    Now you can do something like follows:-

    this.clock = this._store.select('clock');


Wow, this is a big topic. So basically "select" is really a RXJS operator that is used in this case to retrieve the value of a part of the application state object. So say your main app state has a array of users and a array of security functions. "Select" allows you to get a reference to a observable whose value is just that array of users. Before you get into ngrx you really need to study up on Observables and RXJS. I found this article linked off of the main Github project page for ngrx helpful.

https://gist.github.com/btroncone/a6e4347326749f938510

RXJS and redux can be a big topic but I suggest working on your knowledge in small bite size chunks. It took me about 2 months of working with it before I really started to feel comfortable. Even if you don't stay with ngrx, understanding how RXJS works is incredibly useful and is worth the time investment to learn it.

Here is a gist article that also gives a good intro into RXJS.https://gist.github.com/staltz/868e7e9bc2a7b8c1f754


It returns the state called 'clock'.

Here is an example. In the constructor store.select is called, this time with 'todos'.

https://github.com/btroncone/ngrx-examples/blob/master/todos/src/app/todo-app.ts

export class TodoApp {    public todosModel$ : Observable<TodoModel>;    //faking an id for demo purposes    private id: number = 0;    constructor(        private _store : Store<AppState>    ){        const todos$ = _store.select<Observable<Todo[]>>('todos');        const visibilityFilter$ = _store.select('visibilityFilter');

...

In the bootstrap, provideStore is given APP_REDUCERS

import {bootstrap} from '@angular/platform-browser-dynamic';import {TodoApp} from './todo-app';import {provideStore} from "@ngrx/store";import * as APP_REDUCERS from "./reducers/reducers";export function main() {  return bootstrap(TodoApp, [      provideStore(APP_REDUCERS)  ])  .catch(err => console.error(err));}

APP_REDUCERS is all the reducers defined. The todos reducer is defined as follows:

    import {ActionReducer, Action} from "@ngrx/store";import {Todo} from "../common/interfaces";import {ADD_TODO, REMOVE_TODO, TOGGLE_TODO} from "../common/actions";export const todos : ActionReducer<Todo[]> = (state : Todo[] = [], action: Action) => {  switch(action.type) {      case ADD_TODO:          return [              ...state,              action.payload          ];

There are a few ways to do this, and you can compose a list of all your reducers, essentially defining a series of object keys that refer to a reducer object.

Store.select returns an observable that you can subscribe to either in your component or template via '|async'.