Redux Saga Watch Multiple Action

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.

Answers:

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.