redux-saga-从node_modules导入sagas并将其与其他sagas合并

时间:2018-07-29 14:12:49

标签: redux-saga

是否可以将node_modules中的saga与我为我的应用程序编写的其他saga结合起来?如果sagaMiddleware.run()接受sagas数组,但id不接受,则很有意义

我在具有此传奇的node_modules中有npm模块。

// node_modules/module/rootSaga.js
import { takeLatest } from 'redux-saga/effects'
import { createUserSaga } from './sagas/usersSagas'

export default function* usersModuleMernSagas() {
    yield takeLatest('CREATE_USER_REQUEST', createUserSaga)
}

我的商店看起来像这样

// store.js
import { applyMiddleware, createStore } from 'redux'
import createSagaMiddleware from 'redux-saga'
import { routerMiddleware } from 'react-router-redux'
import history from 'history-module-mern'

import rootSaga from 'module/rootSaga'  // this is taht import from node_modules
import rootReducer from './rootReducer'
const sagaMiddleware = createSagaMiddleware()
const historyMiddleware = routerMiddleware(history)
const middleware = applyMiddleware(sagaMiddleware, historyMiddleware)

export const store = createStore(
    rootReducer,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
    middleware
)

sagaMiddleware.run(rootSaga) //this can run only one saga

1 个答案:

答案 0 :(得分:1)

为了将多个Sagas传递到sagaMiddleware.run(),您需要创建一个单独的生成器,在其中将产生所有Sagas。

下面您可以看到带有2个sagas的示例:

// sagas.js

import { all, fork } from "redux-saga/effects";
import appSagas from "./App";
import authSagas from "./Auth";

export default function* () {
  yield all([
    fork(appSagas),
    fork(authSagas)
  ]);
}

然后,将生成器函数导入为sagas,然后将其作为一个参数传递给sagaMiddleware.run(),如下所示:

// index.js

import sagas from "./sagas";

sagaMiddleware.run(sagas);