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

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

问题描述

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天全站免登陆