infodev
infodev

Reputation: 5235

use dynamic ngrx selector or ngrx action for better performance?

I would get data from store depending on a parameter

I have created the selector but with static parameter

export const selectSchedulingsTimes = createSelector(
  schedulings,
  (state: ISchedulesState, { time }: { time: string }) => {
    let nowFormat = moment(time, 'HH:mm');
    return state.schedulings.data.filter(elm => {
      let before = moment(elm.tp_org_r, 'HH:mm:ss');
      let after = moment(elm.tp_des_r, 'HH:mm:ss');

      let checkifBetween = nowFormat.isBetween(before, after);

      if (checkifBetween) {
        return elm;
      }
    });
  }
);

then passing parameter in my component

export class LineSynopticComponent implements OnInit, AfterViewInit {

  schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: '10:34' }));

ngOninit(){
}

as schedules$ is not declared inside ngOnOnit if I set a variable instead of 10:34 will it be working ?

Is it better to use a reducer the send back the values or selector are more performant ?

Upvotes: 0

Views: 1313

Answers (1)

ukn
ukn

Reputation: 1793

There is no problem calling a selector with a parameter. Just make sure the parameter exist when you call your selector. I would do something like this:

  • Declare your observable.
  • Call the selector where your parameters is going to be available, I used onInit since this is the only thing you provided.

    schedules$: Observable<any> // whatever type it should be returning
    
    ngOninit(){
       this.schedules$ = this.store.pipe(select(selectSchedulingsTimes, { time: '10:34' }));
    }
    

Upvotes: 2

Related Questions