Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to filter state?

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

My question is where do I filter the state if I want a read only subset of it to be used in the component?

Do I do it in the reducer, service, or component?

like image 235
user3253156 Avatar asked Apr 08 '16 17:04

user3253156


People also ask

What is a filter state?

Small solid-state filters, discriminators, and multiplexers that contain yttrium-iron-garnet crystals used in combination with a variable magnetic field to accomplish wideband tuning in microwave circuits. From: Modern Dictionary of Electronics (Seventh Edition), 1999.

How do you filter a state array in react?

To filter an array of objects in React: Call the filter() method on the array. On each iteration, check if a certain condition is met. The Array. filter methods returns an array with all elements that satisfy the condition.

What is filter state its need?

A filter is a circuit capable of passing (or amplifying) certain frequencies while attenuating other frequencies. Thus, a filter can extract important frequencies from signals that also contain undesirable or irrelevant frequencies. In the field of electronics, there are many practical applications for filters.

How do you filter data in react?

Filtering in React The use of filter() translates one-to-one to the React world. Given the filtering functions and general example above, you can create the following component: // ... const App = () => { const [input] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); return ( <div> <div>Even: {input.


1 Answers

Some guidance can be found in the ngrx example application. There is a pattern in which selectors are defined alongside reducers:

/**
 * 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 those selectors are used in (smart) components to select/filter the state:

...
export class CollectionPage {

  books$: Observable<BooksInput>;

  constructor(store: Store<AppState>) {
    this.books$ = store.let(getBookCollection());
  }
}

This pattern/mechanism could be used to filter the state in either components or services - whichever best suits your architecture.

like image 197
cartant Avatar answered Oct 14 '22 10:10

cartant