Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a parameter to a @ngrx/component-store selector?

Is there a way (or is it even appropriate) to pass a parameter to @ngrx/component-store selector? I was excited about component-store because effects and updaters can take an Observable as a parameter. This means that that Observable does not have to be subscribed to in a component. But there is not appeared to be a way to use an Observable as a parameter to a selector.

like image 641
Joseph Genchik Avatar asked Sep 05 '25 03:09

Joseph Genchik


2 Answers

 selectMovie(movieId: string) {
    return this.select((state) => state.movies.find(m => m.id === movieId));
 }

I still wonder how to pass movieId as an Observable and return values from selector when movieId or state was modified.

like image 90
Joseph Genchik Avatar answered Sep 07 '25 22:09

Joseph Genchik


Judging by one of the signatures:

  select<R, S1>(
    s1: Observable<S1>,
    projector: (s1: S1) => R,
    config?: SelectConfig
  ): Observable<R>;

I think you can achieve what you're looking for by doing:

select(observable1$, () => {})

You can have use more than one observable, as long as the last arguments are (in this order) either the projectioFn and configObject or only the projectionFn.

like image 38
Andrei Gătej Avatar answered Sep 07 '25 22:09

Andrei Gătej