使用CombineReducer的Redux-saga存储配置错误

时间:2019-06-21 04:29:56

标签: redux redux-saga reducers

我尝试使用Redux-saga配置我的saga存储,但我不断收到有关以下内容的错误 Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.

我在Google周围检查了文档,调整了我的代码仍然存在问题,因此希望有人可以为我指出。谢谢!

如果我更改此代码 const sagaMiddleware = ReduxSaga.default();const sagaMiddleware =()=> ReduxSaga.default(); 我收到其他有关以下错误:sagaMiddleware.run is not a function

Main.js

const { createStore, applyMiddleware } = require("redux");
const { createSelector } = require("reselect");
const ReduxSaga = require("redux-saga");
const createSagaMiddleware = require("redux-saga");
const reducer = require("./reducers/index");
const { workerSaga } = require("./sagas/sampleSaga.js");

const sagaMiddleware = ReduxSaga.default();
const store = createStore(reducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(workerSaga);

Reducers / index.js

const { combineReducers } = require("redux");
const sample1 = require("./sample1");
const sample2 = require("./sample2");
const rootReducer = combineReducers({
  sample1,
  sample2
});
module.exports = rootReducer;

Reducers / sample1.js

const {
  STARTS_LOADING_SAMPLE,
  FETCH_SAMPLE,
  COMPLETE_FETCHING_SAMPLE
} = require("../actions/index");

const sampleInitialState = {
  fetching: false,
  complete: false,
  sample1: []
};
//switch statement....

module.exports = {
  sample1: (allReducer, sampleInitialState)
};

1 个答案:

答案 0 :(得分:0)

reduceer是一个纯函数,它采用上一个状态和一个动作,然后返回下一个状态。但是,您正在导出Reducers/sample1.js文件中的对象而不是函数。尝试将其更改为以下内容:

...
module.exports = function(state = sampleInitialState, action) {
   // switch statements
};

有关减速器的更多信息:https://redux.js.org/basics/reducers