How to create rootSaga?
According to a core developer of redux-saga [1,2] the idiomatic way to create rootSaga is to use the all Effect Combinator. Also, please note that yielding arrays from sagas is deprecated.
Example 1
You could use something like this (+all)
import { fork, all } from 'redux-saga/effects';
import firstSaga from './firstSaga';
import secondSaga from './secondSaga';
import thirdSaga from './thirdSaga';
export default function* rootSaga() {
yield all([
fork(firstSaga),
fork(secondSaga),
fork(thirdSaga),
]);
}
Example 2
Taken from here
// foo.js
import { takeEvery } from 'redux-saga/effects';
export const fooSagas = [
takeEvery("FOO_A", fooASaga),
takeEvery("FOO_B", fooBSaga),
]
// bar.js
import { takeEvery } from 'redux-saga/effects';
export const barSagas = [
takeEvery("BAR_A", barASaga),
takeEvery("BAR_B", barBSaga),
];
// index.js
import { fooSagas } from './foo';
import { barSagas } from './bar';
export default function* rootSaga() {
yield all([
...fooSagas,
...barSagas
])
}
fork vs. spawn
fork and spawn will both return Task objects. Forked tasks are attached to parent, whereas spawned tasks are detached from the parent.
Based on above, you could, use fork for "mission critical" tasks, i.e. "if this task fails, please crash the whole app", and spawn for "not critical" tasks, i.e. "if this task fails, do not propagate the error to the parent".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…