23

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.

Haider Ali
  • 1,275
  • 7
  • 20
Daman
  • 473
  • 2
  • 7
  • 17

1 Answers1

42

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

John Weisz
  • 30,137
  • 13
  • 89
  • 132
leonprou
  • 4,638
  • 3
  • 21
  • 27
  • 8
    Can we do it the opposite way? Like this: `yield takeLatest(LOAD_SEARCHRESULTS, [getSearchResults, getTotalResults]);`. So as we listen to one action and when it fires we call two methods. – jake-ferguson Jul 22 '19 at 05:23
  • 3
    @jake-ferguson takeLatest defined as `takeLatest(pattern, saga, ...args)`, when saga is a generator function. So passing multiple sagas seems to be unsupported. I think the only way is to wrap these two sagas into one. – leonprou Jul 22 '19 at 17:07