My Saga Root looks like this
export default function* root() {
yield takeLatest(LOAD_SEARCHRESULTS, getSearchResults);
}
it watches LOAD_SEARCHRESULTS action and then calls getSearchResults function.
Is there any way I can watch multiple actions in root? Something like this:
export default function* root() {
yield takeLatest(LOAD_SEARCHRESULTS, getSearchResults);
yield takeLatest(CHANGE_ALIASFILTER, getSearchResults);
yield takeLatest(CHANGE_CATFILTER, getSearchResults);
}
So if any of those action comes in - it calls getSearchResults. I have tried yield all([]) and takeEvery but it only watches for first action.
Unlike takeEvery , takeLatest allows only one fetchData task to run at any moment. And it will be the latest started task. If a previous task is still running when another fetchData task is started, the previous task will be automatically cancelled.
Such a powerful & elegant tool as Redux-Saga, a Redux side effect manager, is said to be deprecated, and no longer being maintained, starting from Jan 27, 2021.
Since you're using takeEvery , no, there's no way to dispatch a GET_USER action from within your saga without triggering an infinite loop.
Create a plain JavaScript Object to instruct the middleware that we need to dispatch some action, and let the middleware perform the real dispatch. This way we can test the Generator's dispatch in the same way: by inspecting the yielded Effect and making sure it contains the correct instructions.
takeLatest can also take an array of actions, so you just need to do
export default function* root() {
yield takeLatest([LOAD_SEARCHRESULTS, CHANGE_ALIASFILTER, CHANGE_CATFILTER], getSearchResults);
}
Another option is to use all
and fork
, like here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With