Redux-saga:配置商店时出错

时间:2018-09-21 14:17:36

标签: react-native redux redux-saga

我正在尝试创建一个商店并将redux-saga中间件应用于该商店。我已经配置了所有东西,但是当我运行项目时,会弹出以下错误。

***Error: Before running a saga, you must mount the saga middleware on the store
using applyMiddleware

我的错误发生在第sagaMiddleware.run(sagas);行上。

  

store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import sagas from '../redux/sagas';

const logger = createLogger({
  predicate: (getState, action) => isDebuggingInChrome,
  collapsed: true,
  duration: true,
  diff: true,
});

export default function configureStore() {
  const sagaMiddleware = createSagaMiddleware();
  const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

  const store = createStore(
    applyMiddleware(sagaMiddleware, logger),
  );

  sagaMiddleware.run(sagas);

  return store;
}

知道我在做什么错吗?

react-native: 0.57.0
redux-saga: ^0.16.0
redux: ^4.0.0

1 个答案:

答案 0 :(得分:1)

那是因为您没有正确创建store,请注意您没有通过rootReducer

createStore是一种增强器,它返回一个以rootReducer作为参数的函数,其结果为store

您可能想做这样的事情,相反:

import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import sagas from '../redux/sagas';
import rootReducer from '../redux/rootReducer';

const logger = createLogger({
  predicate: (getState, action) => isDebuggingInChrome,
  collapsed: true,
  duration: true,
  diff: true,
});
const enhancers = process.env.NODE_ENV !== 'production' && window.devToolsExtension
  ? [window.devToolsExtension()]
  : [];

export default function configureStore() {
  const sagaMiddleware = createSagaMiddleware();

  const store = compose(
    applyMiddleware(sagaMiddleware, logger),
    ...enhancers
  )(createStore)(rootReducer);

  sagaMiddleware.run(sagas);

  return store;
}