如何组合多个Redux Saga调用

问题描述 投票:0回答:1

如何组合我需要在其他页面上单独使用的2个传奇调用?

我已经尝试了一些基于其他SO答案的东西(全部产生等),但我无法达到预期的效果。

我有下面显示的传奇,我的应用程序大多数工作正常。在各个集合和集合页面上,我没有任何问题。但是,我有一个页面,它是Collections和Collection的组合。在返回集合之后,我的Saga将setLoader设置为false在第二个saga调用返回Collection的结果之前意味着我的加载器行为不正确。

我相信我可以为GET_BOTH添加另一个鸭子/中间件,* loadBoth可以完成,但我想避免这种情况。

任何建议将不胜感激。谢谢。

import { fork, call, put, take } from "redux-saga/effects";
import { setLoader } from "../ducks/loader";
import { types as collectionsTypes, actions as collectionsActions } from "../ducks/collections";
import { types as collectionTypes, actions as collectionActions } from "../ducks/collection";
import { 
    getCollectionsApiRequest,
    getCollectionApiRequest
} from "../services/api";

export function* loadInitialCollections(query = "") {
    try {
        yield put(setLoader(true));
        const collectionsResult = yield call(getCollectionsApiRequest, query);

        yield put(collectionsActions.getCollectionsSuccess({
            collections: collectionsResult.data,
            feature: collectionsTypes.COLLECTIONS
        }));
        yield put(setLoader(false));

    } catch(e) {
        console.log(e);
    }
}

export function* loadInitialCollection(query = "") {
    try {
        yield put(setLoader(true));
        const collectionResult = yield call(getCollectionApiRequest, query);

        yield put(collectionActions.getCollectionSuccess({
            collection: collectionResult.data,
            feature: collectionTypes.COLLECTION
        }));
        yield put(setLoader(false));

    } catch(e) {
        console.log(e);
    }
}


export function* watchGetCollectionsRequest() {
    while(true) {
        const { query } = yield take(collectionsTypes.GET_COLLECTIONS);
        yield fork(loadInitialCollections, query);
    }
}

export function* watchGetCollectionRequest() {
    while(true) {
        const { query } = yield take(collectionTypes.GET_COLLECTION);
        yield fork(loadInitialCollection, query);
    }
}

const salesSaga = [
    fork(watchGetCollectionsRequest),
    fork(watchGetCollectionRequest)
];

export default salesSaga;
reactjs react-native redux redux-saga
1个回答
0
投票

而不是在动作put上做collectionActions.getCollectionSuccess,你可以直接使用call调用saga什么是行动解决,谢谢它将等待单位传奇getCollectionSuccess将完成。

© www.soinside.com 2019 - 2024. All rights reserved.