首页 > 解决方案 > 无法从 store 中选择最新的 ID,withLatestFrom 返回以前的 ID 而不是最后一个,为什么?

问题描述

我正在尝试创建一个主/详细页面,我需要主 ID 来检索详细列表的相关记录。

我有一个选择器

export const selectRecords= (state: CoreState) => state.records;

export const getId = createSelector(selectRecords,
    (state: myState) => state && state.selectedRecords && state.selectedRecords.id
);

在我看来,我正在尝试从商店获取最新的 id,例如:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

但即使我正在使用withLatestFrom,我得到的是以前的 ID,而不是最新的。

当我从 Redux 检查控制台时,状态中的 selectedRecords.id 是正确的(最新的),是我想要检索的。

我知道这是一个远景,但有没有人有提示或建议我可以尝试从商店获取正确的最后一个 ID?

标签: angularrxjsngrxtypescript-typings

解决方案


我设法通过在 withLatestFrom 之前添加 debounceTime(0) 来解决问题:

更新工作代码为:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        debounceTime(0),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

我正在添加一个描述我遇到的确切问题的链接: https ://github.com/ReactiveX/RxSwift/issues/1121

换句话说 .withLatestFrom(item.asObservable()) 不能提供实际最新的下一个事件的非零值,但提供前一个的零值!


推荐阅读