Joel
Joel

Reputation: 460

Access store from effect with ngrx

I need to access the store state from an effect in order to use a service and filter a property in the store.

I've read about an operator called withLatestFrom but I have not had any success using it.

How can I access the store in my filterTransports effect?

constructor(private store$: Store<fromFundamentalData.State>) {}


@Effect() filterTransports$ = this.actions$.pipe(
    ofType(fundamentalDataActions.FundamentalDataTypes.FilterTransports),
    mergeMap((action: fundamentalDataActions.FilterTransports) => this.filterTransportsService.filter(action.payload, action).pipe(
        map((transports: any) => (new fundamentalDataActions.FilterSuccess(transports))))
    ));

Upvotes: 4

Views: 2101

Answers (1)

timdeschryver
timdeschryver

Reputation: 15487

You can do it like:


@Effect()
shipOrder = this.actions.pipe(
  ofType<ShipOrder>(ActionTypes.ShipOrder),
  map(action => action.payload),
  concatMap(action =>
    of(action).pipe(
      withLatestFrom(this.store.pipe(select(getUserName)))
    )
  ),
  map([payload, username] => {
    ...
  })
)

For more info see Start using ngrx/effects for this

Upvotes: 3

Related Questions