首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将sagaMiddleWare.run(RootSaga)集成到redux storeFactory中

如何将sagaMiddleWare.run(RootSaga)集成到redux storeFactory中
EN

Stack Overflow用户
提问于 2018-03-25 04:16:48
回答 1查看 1.7K关注 0票数 1

下面的代码是一个存储工厂,它将在给定语句的情况下创建store on命令

代码语言:javascript
复制
const store = storeFactory()

我需要将sagaMiddleWare与此storeFactory集成,但我放置代码的任何位置:

sagaMiddleWare.run(RootSaga)

我收到以下错误:

错误:在运行Saga之前,您必须使用applyMiddleware在商店上挂载Saga中间件

如何将sagaMiddleWare.run(RootSaga)集成到storeFactory中?

代码语言:javascript
复制
"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;
EN

回答 1

Stack Overflow用户

发布于 2018-12-18 15:18:28

这里得到了一个错误,似乎表明saga中间件没有正确加载到存储中。

代码语言:javascript
复制
/* 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.html
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49469471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档