16

Pretty new to redux-observables, rxjs and observables. Wanted to know how can I handle another action, say 'ActionTwo' in the same epic

const Epic1 = (action$,store) => {
return action$.ofType('ActionOne')
 .mergeMap((action) => {
      return ajax({'method': 'GET', 'url': 'someUrl')
         .map(response => resultActoin(action.userId, response.response));


       }
  );
 }

Something like

const Epic1 = (action$){
   if('ActionOne') make a API call.
   if('ActionTwo') make some other API call.
   else do nothing.

}
Ajinkya Salve
  • 213
  • 1
  • 2
  • 6

1 Answers1

40

Is it the same API call? If so, ofType() accepts more than one type. You can just do action$.ofType('ActionOne', 'ActionTwo').

If you want to make a request to another API/URL, I would recommend to make another epic. You can "merge" all you epics with combineEpics see: https://redux-observable.js.org/docs/basics/SettingUpTheMiddleware.html

Sebastian Sebald
  • 16,130
  • 5
  • 62
  • 66
  • 4
    ^^^ this. It's almost always better to have one epic per task – jayphelps Mar 16 '17 at 18:12
  • Knew about combineEpics, was trying to handle multiple actions in one epic. Can you put some light on why having epic/per action is a good design? Other than for code maintainability? – Ajinkya Salve Mar 20 '17 at 11:31
  • 1
    It's only about maintainability. The reasons are the same, why you have multiple files and modules. It helps to scale and reason your applications, without little to any downside. Also, it makes refactoring more easy as well as creating you action streams. – Sebastian Sebald Mar 20 '17 at 12:30
  • My pleasure! :) – Sebastian Sebald Mar 20 '17 at 20:37