需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)


問題描述

需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)

Ngrx 文檔使用從商店狀態中選擇狀態的效果(沒有雙關語)

Note: For performance reasons, use a flattening operator like concatMap to prevent the
selector from firing until the correct action is dispatched.

...並提供了示例:

    addBookToCollectionSuccess$ = createEffect(() => this.actions$.pipe(
      ofType(CollectionApiActions.addBookSuccess),
      concatMap(action => of(action).pipe(
          withLatestFrom(this.store.select(fromBooks.getCollectionBookIds))
      )),
      tap(([action, bookCollection]) => console.log('whatever'))
      ), { dispatch: false }
    );

示例自已更改為使用 concatLatestFrom,但這並不重要。我感興趣的是理解最初的建議。我想我理解了意圖,以避免在源操作觸發時讓 withLatestFrom 處理來自 getCollectionBookIds 的結果,但我不明白為什麼解決方案需要涉及包裝withLatestFrom 使用 of(action) 在手動構造的 Observable 中。不會


參考解法

方法 1:

Your suggestions should work fine, if you just need the selected state:

concatMap(() => this.store.select(selectSelectedBookIds).pipe(first())),

However, should you also need the action itself, the following will pass along an array containing both the action and your selected state:

concatMap(action => of(action).pipe(
  withLatestFrom(this.store.select(fromBooks.getCollectionBookIds))
)),

(by NeutrinoSteve Holgado)

參考文件

  1. Need help understanding this Ngrx effect (CC BY‑SA 2.5/3.0/4.0)

#ngrx-effects #ngrx #ngrx-store #rxjs






相關問題

rxjs減少不繼續 (rxjs reduce does not continue)

從 reducer/action 觸發 API 重新加載的正確方法是什麼? (What's the proper way to trigger the API reload from reducer/action?)

Angular NgRx 效果,如何傳遞參數? (Angular NgRx effects, how to pass a parameter?)

NgRx 效果無限循環 (NgRx Effects infinite loop)

發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

使用 NgRx 存儲優化 Angular 內存以避免內存洩漏 (Angular memory optimization with NgRx store to avoid memory leaks)

NgRx EffectsModule 導入組 (NgRx EffectsModule imports group)

切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)

如何防止在 NgRx 中第二次加載數據? (How prevent loading data second time in NgRx?)

需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)

測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)







留言討論