使用 combineReducer 的 Redux-saga 存储配置错误
Redux-saga store config error with combineReducer
我尝试使用 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)
};
reducer 是一个纯函数,它接受前一个状态和一个动作,returns 接受下一个状态。但是您在 Reducers/sample1.js
文件中导出对象而不是函数。尝试将其更改为这样的内容:
...
module.exports = function(state = sampleInitialState, action) {
// switch statements
};
关于减速器的更多信息:https://redux.js.org/basics/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)
};
reducer 是一个纯函数,它接受前一个状态和一个动作,returns 接受下一个状态。但是您在 Reducers/sample1.js
文件中导出对象而不是函数。尝试将其更改为这样的内容:
...
module.exports = function(state = sampleInitialState, action) {
// switch statements
};
关于减速器的更多信息:https://redux.js.org/basics/reducers