在可观察的史诗

时间:2018-04-18 13:20:42

标签: rxjs redux-observable

我需要将商店值传递给史诗上的switchMap函数,但似乎只传递了操作并且存储为0.

export const addVideoEpic = (action$, store) =>
      action$.ofType(videoTypes.ADD_VIDEO_REQUEST)
             .filter(() => of(store.getState()))
             .switchMap((action, store) => {
               const data = {
                url: store.url
               };
               console.log(store) // 0
               return from(addVideo(data))
             });

如何将商店值传递给switchMap

1 个答案:

答案 0 :(得分:1)

为了将商店传递到史诗中,您的史诗必须知道商店的存在。在商店创建课程中,请确保您具有以下内容:

const epicMiddleware = createEpicMiddleware();
const persistedReducer = persistReducer(persistConfig, rootReducer as Reducer)

export const store = createStore(
 persistedReducer,
 applyMiddleware(epicMiddleware)
);

epicMiddleware.run(rootEpic);

并假设您的redux-observable版本是^1.0.0或更高版本,而rxjs^6.0.0或更高版本,则可以访问存储以及存储的任何状态:

export function fetchRandomStuffEpic(
  action$: ActionsObservable<RandomAction>,
  store$: StateObservable<RootState>
) {
     return action$.pipe(
     ofType(RANDOM),
     switchMap(action => {

      // store.getState() no longer works in the newer versions
      // and has to accessed as a value property with store$
      const { authToken } = store$.value.userData;

     // whatever code that follows
    })
 );
}