状态更新后ngrx效果是否再次触发?

问题描述 投票:0回答:1

在我的 Angular 项目中,我有这个 ngrx 效果,它会触发一个新的 SearchAction:

createSearchWithNewLimit$ = createEffect(() => {
    return this.actions$.pipe(
      ofType(pageLimitChangedAction),
      concatLatestFrom(() => [this.store.select(selectSearchParams)]),
      switchMap(([{limit}, currSearchParams]) => {
        if(currSearchParams) {

          const searchParams = { ...currSearchParams, limit};

          return of(searchAction({searchParams}));
        }
        return EMPTY;
      })
    )
  })

此外,SearchAction 减速器会更新状态中的 searchParams(在 this.store.select(selectSearchParams) 中在此效果中检索的 searchParams)。

这是否意味着每次调度 SearchAction 时,都会触发此效果,因为它使用的是 this.store.select(selectSearchParams)?如果是这样,这怎么会不会导致无限循环?或者只评估一次? 谢谢

angular rxjs observable ngrx
1个回答
0
投票

concatLatestFrom
从存储中读取当前值并将该值传递给下一个运算符。当它的值改变时,它不会重新触发效果。

它与 RxJS 的

withLatestFrom
运算符相当,不同之处在于
concatLatestFrom
是延迟计算的。

有关更多信息,请参阅文档:

注意:出于性能原因,请使用 concatLatestFrom 等扁平化运算符来防止选择器在分派正确操作之前触发。

© www.soinside.com 2019 - 2024. All rights reserved.