Search code examples
reactjsreduxredux-saga

Best way to write a rootSaga


export function* watcherSaga() {
    yield all([
        takeEvery("POST_CONTACT_USER", handlePostContactUser),
        takeEvery("POST_CAREERS_USER", handlePostCareersUser),
        takeEvery("POST_TEAM", handlePostTeam),
        takeEvery("CONTACT_SEND_TO_NODE", handleSendContactToNode),
        takeEvery("CAREERS_SEND_TO_NODE", handleSendCareersToNode),
        takeEvery("TEAM_SEND_TO_NODE", handleSendTeamToNode),
        takeEvery("PROJECT_SEND_TO_NODE", handleSendProjectToNode),
        takeEvery("POST_PROJECT", handlePostProject),
    ]);
};

This is the way I'm handling it now but I feel like it might not be the best approach, sometimes stuff doesn't run and I don't think I have broken code in other parts of the project (I'm not getting any error either).

Is this the best approach there is and I'm missing something else or is indeed this code buggy?


Solution

  • This approach is fine and shouldn't cause any bugs by itself. The only "weakness" is that if any of the child sagas throws an error, it will kill the whole saga tree.

    You can prevent that using the spawn effect that will create completely new saga tree, however you should still catch and handle these errors.

    The redux-saga documentation has actually a dedicated page for this including some examples how to automatically restart a saga subtree if it fails:

    https://redux-saga.js.org/docs/advanced/RootSaga/