• Reading your Application State using Selectors
    • Creating a Counter Service

    Reading your Application State using Selectors

    To read your application state in Redux, we need to use the select() method on @ngrx’s Store class. This method creates and returns an Observable that is bound to a specific property in your application state.

    For example, here’s how you would select the counter object:

    1. store.select('counter'); // Returns Observable<Counter>

    And to fetch the counter’s currentValue, we can pass in a string array, where each string plucks a single property from the application state one at a time in the order specified:

    1. store.select(['counter', 'currentValue']); // Returns Observable<number>

    While select() allows for several variations of strings to be passed in, it has it’s shortcomings - namely you won’t actually know if the plucking is working properly until you execute your code.

    Because of that, select() allows you to select values using functions too, which makes things more type-safe and your selectors will be more refactorable by your IDE.

    1. store.select(appState => appState.counter.currentValue);

    Creating a Counter Service

    While you could inject Store and select values directly in your Angular components, it’s considered to be a best practice to wrap this functionality into separate services. This approach encapsulates all of the selection logic and eliminates any duplication where the selection path is repeated throughout your application.

    Let’s tie everything together by building out a CounterService example:

    app/services/counter.service.ts

    1. import {Injectable} from '@angular/core';
    2. import {Store} from '@ngrx/store';
    3. import {Observable} from 'rxjs/Observable';
    4. import {AppState} from '../models';
    5. @Injectable()
    6. export class CounterService {
    7. constructor(private store: Store<AppState>) {}
    8. getCurrentValue(): Observable<number> {
    9. return this.store.select(appState => appState.counter.currentValue)
    10. .filter(Boolean);
    11. }
    12. }

    Because select() returns an Observable, the getCurrentValue() method also applies a filter() to ensure that subscribers do not receive any falsy values. This greatly simplifies the code and templates in your components, since they don’t have to repeatedly consider the falsy case everywhere the value is used.