DevLoverUmar
DevLoverUmar

Reputation: 13923

How to get the store state in Effects using withLatestFrom operator

I'm trying with this

payloadToBeSaved$ = createEffect(() =>  this.actions$
.ofType(SOME_ACTION)
.withLatestFrom(this.store$)
.map(([action: Action, storeState: AppState]) => {
   // Do something ...
});

But this.actions$ is an observable and I have to use this.actions$.pipe().... when I try with pipe() all lines red with errors. Don't know how to fix.

Upvotes: 1

Views: 411

Answers (1)

timdeschryver
timdeschryver

Reputation: 15487

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

Reference: Start using ngrx/effects for this

Upvotes: 2

Related Questions