如何使用Redux Saga运行Redux DevTools?

时间:2018-10-04 06:14:07

标签: redux react-redux redux-saga

尝试通过redux saga运行reduxdevtools:

出现此错误:

Error
Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware

这是我的jscode:

const store = createStore(
  reducer,
  applyMiddleware(sagaMiddleware),
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

如何使用saga运行此devtool?或者,否则会起作用吗? codepen

4 个答案:

答案 0 :(得分:4)

我使用了redux-devtools-extension package,如此处所述,redux-devtools-extension documentation

添加软件包后,我将商店定义替换为:

const store = createStore(
  reducer,
  composeWithDevTools(
    applyMiddleware(sagaMiddleware)
  )
);

Fixed Codepen Link

答案 1 :(得分:0)

先前的答案(由trkaplan提出)使用了来自“ redux-devtools-extension”包的导入方法composeWithDevTools。  如果您不想安装此软件包,则可以使用以下代码(基于the docs):

      const composeEnhancers =  typeof window === 'object' && window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] ? 
      window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__']({ }) : compose;
      const enhancer = composeEnhancers(
        applyMiddleware(thunkMiddleware, sagaMiddleware, /*other middleware*/),
        /* other store enhancers if any */
      );
      const emptyReducer = () => {};
      const store = createStore(emptyReducer, enhancer);

答案 2 :(得分:0)

如果使用Redux编写。然后下面的代码很有用。 步骤1:添加chrome Redux DevTools扩展。 步骤2:npm安装redux-devtools-extension。

    import { composeWithDevTools } from 'redux-devtools-extension';
    const store = createStore(
                  reducer,
                  compose(
                  applyMiddleware(sagaMiddleware),
                  composeWithDevTools(),
                  ),
                  );

答案 3 :(得分:0)

这是您为真实项目配置 redux、redux-devtool-extension 和 redux-saga 的方式..

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import createSagaMiddleware from 'redux-saga';

import rootReducer from '../reducers';
import rootSaga from '../sagas';

const configureStore = () => {
    const sagaMiddleware = createSagaMiddleware();
    return {
        ...createStore(rootReducer, composeWithDevTools(applyMiddleware(sagaMiddleware))),
        runSaga: sagaMiddleware.run(rootSaga),
    };
};

export default configureStore;