redux-saga - import and combine sagas from node_modules with other sagas

北城余情 提交于 2020-02-28 17:20:10

问题


Is there any way to combine sagas from node_modules with other sagas i wrote for my app? It would make sense if sagaMiddleware.run() would accept array of sagas, but id doesn't

I have npm module in node_modules which has this saga.

// 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)
}

My store look like this

// 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:


In order to pass multiple sagas to sagaMiddleware.run() you need to create a separate generator where you will yield all of your sagas.

Below you can see my example with 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)
  ]);
}

Then I import my generator function as sagas and just pass it as one parameter to the sagaMiddleware.run() like so:

// index.js

import sagas from "./sagas";

sagaMiddleware.run(sagas);


来源:https://stackoverflow.com/questions/51581181/redux-saga-import-and-combine-sagas-from-node-modules-with-other-sagas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!