如何异步添加史诗?

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

我能够添加异步reducer,但无法添加异步史诗

在此链接Adding New Epics Asynchronously之后,我尝试使用epic$.next()但无法添加新的史诗。

import { applyMiddleware, createStore, compose } from 'redux';
import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { BehaviorSubject } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import createReducer from '../reducers';
import mainEpic from '../config/epics';

中间件配置如下:

const epicMiddleware = createEpicMiddleware();

const epic$ = new BehaviorSubject(mainEpic);
const rootEpic = (action$, state$) =>
  epic$.pipe(mergeMap(epic => epic(action$, state$)));

针对redux dev工具的Store Enhancers

const composeEnhancers =
  // compose;
  process.env.NODE_ENV === 'development' &&
  window.navigator.platform !== 'iPad' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    : compose;

这是商店的实例化

export default () => {
  const store = createStore(
    createReducer(),
    composeEnhancers(applyMiddleware(epicMiddleware))
  );

  epicMiddleware.run(rootEpic);

  // Extra functionality to the store
  store.asyncReducers = {};

在这里,当新组件异步绑定时,我用参数keyreducernewEpic调用injectRepics(用于缩减器和史诗的Repics)

  store.injectRepics = (key, reducer, newEpic) => {
    if (!store.getState()[key]) {

      // here I get newEpic
      console.log(newEpic);

      // new reducer is added to asyncreducer object
      store.asyncReducers[key] = reducer;
      // new reducer is created and replaced
      store.replaceReducer(createReducer(store.asyncReducers));

      // -------------------------------------
            But, I'm unable to replace epic
      // -------------------------------------
      newEpic && epic$.next(newEpic);

      // epicMiddleware.run(rootEpic);
      // newEpic && addNewEpic(newEpic);
    }
  };

  return store;
};
reactjs redux redux-observable rxjs6
1个回答
0
投票

有一个简单的错误。

我忘了在新史诗中添加combineEpics。

所以,代码变成了newEpic && epic$.next(combineEpics(...newEpic))

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