Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux Saga Watch Multiple Action

Tags:

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.

like image 834
Daman Avatar asked Oct 04 '18 19:10

Daman


People also ask

What is difference between takeLatest and takeEvery?

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.

Is Redux-saga deprecated?

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.

Can we dispatch action from Saga?

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.

How do I dispatch actions from Saga?

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.


1 Answers

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

like image 180
leonprou Avatar answered Sep 24 '22 03:09

leonprou