reactjsreduxredux-observablereselectepic

Is it possible to take selector from reselect in epics?


I'm using react-observable and reselect in my React project. All I want to achieve is something like that:

// SELECTORS

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

and now in Epics, I want to make something like that:

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);
      },
    ),
  );

Is it somehow possible? Thank you for help!


Solution

  • I noticed how to use it. So if I want to receive what I've got in selector I need to do something like that:

    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);
          },
        ),
      );