How to handle multiple action types in one epic? A

2019-02-16 10:51发布

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条回答
Summer. ? 凉城
2楼-- · 2019-02-16 11:37

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

查看更多
登录 后发表回答