如何在一部史诗中处理多种动作类型?这样做有什么缺点吗? [英] How to handle multiple action types in one epic? Any cons of doing the same?

查看:11
本文介绍了如何在一部史诗中处理多种动作类型?这样做有什么缺点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 redux-observables、rxjs 和 observables 来说是个新东西.想知道我如何处理另一个动作,在同一个史诗中说ActionTwo"

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));


       }
  );
 }

类似的东西

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

}

推荐答案

是同一个 API 调用吗?如果是这样,ofType() 接受不止一种类型.你可以只做 action$.ofType('ActionOne', 'ActionTwo').

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

如果您想向另一个 API/URL 发出请求,我建议您制作另一个史诗.您可以使用 combineEpics合并"所有史诗,请参阅:https://redux-observable.js.org/docs/basics/SettingUpTheMiddleware.html

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

这篇关于如何在一部史诗中处理多种动作类型?这样做有什么缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆