下面的代码是一个存储工厂,它将在给定语句的情况下创建store on命令
const store = storeFactory()我需要将sagaMiddleWare与此storeFactory集成,但我放置代码的任何位置:
sagaMiddleWare.run(RootSaga)
我收到以下错误:
错误:在运行Saga之前,您必须使用applyMiddleware在商店上挂载Saga中间件
如何将sagaMiddleWare.run(RootSaga)集成到storeFactory中?
"use strict";
import { createStore, combineReducers, applyMiddleware } from "redux";
import createSagaMiddleware from "redux-saga";
import { user, venues, isFetching } from "./reducers";
import stateData from "./initialState.json";
import RootSaga from "./sagas";
const logger = store => next => action => {
let result;
console.groupCollapsed("dispatching", action.type);
console.log("prev state", store.getState());
console.log("action", action);
result = next(action);
console.log("next state", store.getState());
console.groupEnd();
return result;
};
const saver = store => next => action => {
let result = next(action);
localStorage["redux-store"] = JSON.stringify(store.getState());
return result;
};
const sagaMiddleWare = createSagaMiddleware();
const storeFactory = (initialState = stateData) =>
applyMiddleware(logger, saver, sagaMiddleWare)(createStore)(
combineReducers({ user, venues, isFetching }),
localStorage["redux-store"]
? JSON.parse(localStorage["redux-store"])
: stateData
);
export default storeFactory;发布于 2018-12-18 15:18:28
这里得到了一个错误,似乎表明saga中间件没有正确加载到存储中。
/* eslint-disable no-underscore-dangle */
const sagaMiddleware = createSagaMiddleware();
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION__ || compose;
const store = createStore(
reducers,
initialState,
composeEnhancers(applyMiddleware(sagaMiddleware)),
);
sagaMiddleware.run(sagas);
/* eslint-enable */
The final code of this tutorial is located in the sagas branch. Then in the .... Now we only have to invoke sagaMiddleware.run on the root Saga in main.js .
https://redux-saga.js.org/docs/introduction/BeginnerTutorial.htmlhttps://stackoverflow.com/questions/49469471
复制相似问题