How to handle multiple action types in one epic? Any cons of doing the same?

元气小坏坏 提交于 2019-11-30 23:13:24

问题


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.

}

回答1:


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



来源:https://stackoverflow.com/questions/42832974/how-to-handle-multiple-action-types-in-one-epic-any-cons-of-doing-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!