1

我在我的 React 项目中使用 react-observable 和 reselect。我想要实现的是这样的:

// 选择器

export const getItem = createSelector(getItemsState, state => state.item);

现在在 Epics 中,我想做这样的事情:

const addItem$: Epic = (action$: Observable<Actions>, state$) =>
  action$.pipe(
    filter(action => action.type === 'ADD_ITEM'),
    withLatestFrom(state$.select(getItem)), // Just use there my selector which is written.
    switchMap((item) => {
        return ItemsApi.addItem(item);
      },
    ),
  );

有可能吗?谢谢你的帮助!

4

1 回答 1

0

我注意到如何使用它。所以如果我想收到我在选择器中得到的东西,我需要做这样的事情:

const addItem$: Epic = (action$: Observable<Actions>, state$) =>
  action$.pipe(
    filter(action => action.type === 'ADD_ITEM'),
    withLatestFrom(getItem(state$.value)),
    switchMap(([, item]) => {
        return ItemsApi.addItem(item);
      },
    ),
  );
于 2020-05-09T16:42:59.673 回答