Where to filter the state?

Newbie question: I have an angular2 application using ngrx, I have a service that returns the state (an array of observables) to the component.

My question is, where can I filter the state if I want to use only a subset of it in it?

Am I doing this in a gearbox, service or component?

+5
source share
1 answer

Some recommendations can be found in the ngrx app. There is a pattern in which selectors are defined next to the gears :

/** * Because the data structure is defined within the reducer it is optimal to * locate our selector functions at this level. If store is to be thought of * as a database, and reducers the tables, selectors can be considered the * queries into said database. Remember to keep your selectors small and * focused so they can be combined and composed to fit each particular * use-case. */ export function getBookEntities() { return (state$: Observable<BooksState>) => state$ .select(s => s.entities); }; 

And these selectors are used in (smart) components to select / filter state:

 ... export class CollectionPage { books$: Observable<BooksInput>; constructor(store: Store<AppState>) { this.books$ = store.let(getBookCollection()); } } 

This template / mechanism can be used to filter state in components or services - depending on what suits your architecture best.

+2
source

Source: https://habr.com/ru/post/1246727/


All Articles