如何组合我需要在其他页面上单独使用的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;
而不是在动作put
上做collectionActions.getCollectionSuccess
,你可以直接使用call
调用saga什么是行动解决,谢谢它将等待单位传奇getCollectionSuccess
将完成。