[我如何并行put
多次相同的动作类型,并能够take
所有响应?在Redux的文档中,它们使用call
和all
并行进行API调用,但是据我所知,reduce仅在分派动作时才调用。如何并行调度相同的动作,获取每个动作的结果,并调用reducer?
# api/actions.ts
const getUserRequest = createAction('GET_USER_REQUEST', { userId });
const getUserSuccess = createAction('GET_USER_SUCCESS', { user });
# api/sagas.ts
function* fetchUser(action) {
const user = axios.get(`api.example.com/users/${action.payload.userId}`;
yield put({type: GET_USER_SUCCESS, user.data});
}
# api/reducers.ts
function (state, action) {
switch(action.type) {
case: GET_USER_REQUEST:
return {
...state,
[action.payload.userId] = {}
};
case: GET_USER_SUCCESS:
return {
...state,
[action.payload.userId] = {...action.payload}
};
default:
return state;
}
}
# ui/sagas.ts
function* fetchSelectedUsers(userIds: number[]){
# I'm stuck at this saga. How can I get multiple users, have reducers invoked, and perform additional processing in this saga after they've been retrieved?
const actionsToDispatch = userIds.map(x =>
put(getUserRequest({userId}))
);
yield all(actionsToDispatch);
# Pseudo code
const users = yield take(actionsToDispatch);
users.map(x => {
// Perform additional work from users that were returned. Reducers must also be called from the `yield all(actionsToDispatch);` from above.
});
}
takeEvery上的通用并发模式可能会在这里为您提供帮助。引用此-https://redux-saga.js.org/docs/advanced/Concurrency.html?_sm_au_=iHV45ngMZPFJrwQH。