为了账号安全,请及时绑定邮箱和手机立即绑定

在 React 中使用 RxJs 链接 Observables 的最佳方式

在 React 中使用 RxJs 链接 Observables 的最佳方式

繁华开满天机 2023-03-03 15:49:36
在这种情况下,最好的方法是链接 Observables 并SEARCH_QUERY_COMPLETE在所有订阅完成后分派操作?我注意到这forkJoin已被弃用......const launchSearchQuery = () => {    mainDispatch({      type: ActionTypes.LAUNCH_SEARCH_QUERY,    });    if (mainState.searchSection.searchQuery !== "") {      // get order details      orderDetailRepository.getOrderDetails(mainState.searchSection.searchQuery).subscribe((response) => {        searchResultCards.push(mapSearchResultCard(response, DashboardSectionTitles.ORDER_DETAILS));      });      // get customer details      customerDetailRepository.getCustomerDetails(mainState.searchSection.searchQuery).subscribe((response) => {        searchResultCards.push(mapSearchResultCard(response, DashboardSectionTitles.CUSTOMER_DETAILS));      });      // get equipment details      equipmentDetailRepository.getEquipmentDetails(mainState.searchSection.searchQuery).subscribe((response) => {        searchResultCards.push(mapSearchResultCard(response, DashboardSectionTitles.EQUIPMENT_DETAILS));      });      // get equipment return details      equipmentReturnDetailRepository        .getEquipmentReturnDetails(mainState.searchSection.searchQuery)        .subscribe((response) => {          searchResultCards.push(mapSearchResultCard(response, DashboardSectionTitles.EQUIPMENT_RETURN_DETAILS));        });    }        // !!! only execute this when all subscriptions are completed !!!    mainDispatch({        type: ActionTypes.SEARCH_QUERY_COMPLETE,        payload: searchResultCards,    });  };这是蓝图getOrderDetailsexport interface IOrderDetailRepository {  getOrderDetails: (query: string) => Observable<IOrderDetail[]>;}
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

forkJoin你可以这样使用-


const launchSearchQuery = () => {

            mainDispatch({

              type: ActionTypes.LAUNCH_SEARCH_QUERY,

            });

        


            

            if (mainState.searchSection.searchQuery !== "") {

                forkJoin([

                    // get order details

                    orderDetailRepository.getOrderDetails(mainState.searchSection.searchQuery),

                    // get customer details

                    customerDetailRepository.getCustomerDetails(mainState.searchSection.searchQuery),

                    // get equipment details

                    equipmentDetailRepository.getEquipmentDetails(mainState.searchSection.searchQuery),

                    // get equipment return details

                    equipmentReturnDetailRepository.getEquipmentReturnDetails(mainState.searchSection.searchQuery)

                ]).subscribe(([orderDetails, customerDetails, equipMentDetails, equimentReturnDetails]) => {

                    searchResultCards.push(mapSearchResultCard(orderDetails, DashboardSectionTitles.ORDER_DETAILS));

                    searchResultCards.push(mapSearchResultCard(customerDetails, DashboardSectionTitles.CUSTOMER_DETAILS));

                    searchResultCards.push(mapSearchResultCard(equipMentDetails, DashboardSectionTitles.EQUIPMENT_DETAILS));

                    searchResultCards.push(mapSearchResultCard(equimentReturnDetails, DashboardSectionTitles.EQUIPMENT_RETURN_DETAILS));

    

                    // !!! only execute this when all subscriptions are completed !!!

                    mainDispatch({

                        type: ActionTypes.SEARCH_QUERY_COMPLETE,

                        payload: searchResultCards,

                    });

                });

            }

        

          };


查看完整回答
反对 回复 2023-03-03
  • 1 回答
  • 0 关注
  • 92 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信