同时启用redux-promise和redux-saga中间件似乎会引起问题

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

我正在使用弹出的create-react-app代码库,并尝试同时设置redux-saga和redux-promise中间件。但是,无论我如何处理,redux-saga似乎都被禁用,或者有时会报告错误,指示未针对sagaMiddleware对象运行applyMiddleware。 (请原谅稍微复杂的示例代码。)

import React from 'react';
import ReactDOM from 'react-dom';
import * as serviceWorker from './serviceWorker';

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import { Route, BrowserRouter, Switch } from 'react-router-dom';

import reducersP1 from './p1/reducers';
import reducersP2 from './p2/reducers';

import Wrapper from './global/containers/wrapper';
import AppWrap from "./p1/components/app_wrap";
import AppWrap2 from "./p2/containers/app_wrap";

import createSagaMiddleware from 'redux-saga';
import rootSaga from './p2/sagas';

const appConfig = {
  p1: {
    reducers: reducersP1,
    component: AppWrap,
    namespace: "p1",
    api: '/api'
  },
  p2: {
    reducers: reducersP2,
    component: AppWrap2,
    namespace: "p2",
    api: '/api2'
  }
};


const { api, reducers, component, namespace } = process.env.REACT_APP_PORTFOLIO==="1"? appConfig.p1: appConfig.p2;
const WrapComponent = component;
export const apiPath = api;


const sagaMiddleware = createSagaMiddleware();

// Approach A.
const createStoreWithMiddleware = applyMiddleware(sagaMiddleware, ReduxPromise)(createStore);
const store = createStoreWithMiddleware(reducers);

// Approach B.
// const storeWithMiddleware = createStore(
//     reducers,
//     applyMiddleware(sagaMiddleware, ReduxPromise)
// );
// const store = storeWithMiddleware;

sagaMiddleware.run(rootSaga);

ReactDOM.render(
  <Provider store={ store }>
    <BrowserRouter>
      <Switch>
        <Route path="*" render={ (history) => <Wrapper namespace={ namespace }><WrapComponent history={ history } /></Wrapper> } />
      </Switch>
      </BrowserRouter>
  </Provider>
  ,
  document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();
javascript reactjs redux redux-saga redux-promise
1个回答
0
投票

似乎redux-saga没有正确安装。固定为:

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