question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root saga pattern - fork vs. run?

See original GitHub issue

I’m a bit confused here, what is the “correct” way to set up a root saga and a bunch of “router” sagas inside?

// Using `fork`?
function* rootSaga () {
    yield [
        fork(saga1), // saga1 can also yield [ fork(actionOne), fork(actionTwo) ]
        fork(saga2),
    ];
}

// Using `middleware.run`?
function* rootSaga () {
    sagaMiddleware.run(saga1); // saga1 can also do middleware.run(actionOne), etc.
    sagaMiddleware.run(saga2);
}

function startRootSaga () {
    sagaMiddleware.run(rootSaga);
}

Also, is there any better way to get store.getState in all of those sagas other than passing it around as a parameter everywhere?

Just for clarity, I’m thinking of this as analogous to an express router where my action types are the routes - is this a good way to think about it?

const app = express();

const router1 = express.Router();
router1.get('/get', get1Fn);

const router2 = express.Router();
router2.get('/get', get2Fn);

const rootRouter = express.Router();
rootRouter.use('/one', router1);
rootRouter.use('two', router2);

app.use(rootRouter);

This express router seems to be analogous to the above “saga router”.

I would like to have just one call in my top level app.js to start the root saga, and then the rest of my sagas are broken up by area of the app, similar to the way I split the router code on the server side.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
yelouaficommented, Apr 25, 2016

run is normal javascipt method that can be used to create and start a task from outside the Sagas. And should be typically used in the same place where you create the store. fork is an Effect that can be only used with yield from inside Sagas.

You shouldn’t use run from inside Sagas just yield fork. And you can’t use fork outside of Sagas.

1reaction
yelouaficommented, Apr 25, 2016

Example of config

./saga.js

// Using `fork`?
function* rootSaga () {
    yield [
        fork(saga1), // saga1 can also yield [ fork(actionOne), fork(actionTwo) ]
        fork(saga2),
    ];
}

./main.js

import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'

import rootReducer from './reducers'
import rootSaga from './sagas'


const sagaMiddleware = createSagaMiddleware()
const store = createStore(
  reducer,
  applyMiddleware(sagaMiddleware)
)
sagaMiddleware.run(rootSaga)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Root Saga
Since fork is non-blocking, the rootSaga can finish while the child sagas continue to run and be blocked by their internal effects.
Read more >
Redux Saga: difference between yield array to fork in the root ...
There are actually two differences here. 1. Using effects vs calling the sagas directly. The redux-saga library can handle yielding ...
Read more >
Redux Toolkit's new listener middleware vs. Redux-Saga
The middleware's primary functionality is let users respond to dispatched actions, or run code in response to state updates.
Read more >
Javascript Power Tools Part III: Real-world redux-saga Patterns
run (rootSaga); const initialRouterState = store.getState().router; store.dispatch(initializeCurrentLocation ...
Read more >
API Reference | redux-saga-ie8
Then in our main module, we will use the method to start the root Saga of the ... fork , alongside race ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found