Root saga pattern - fork vs. run?
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:3
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 withyield
from inside Sagas.You shouldn’t use
run
from inside Sagas justyield fork
. And you can’t usefork
outside of Sagas.Example of config
./saga.js
./main.js