简体   繁体   English

Redux-saga 调用多个函数/动作

[英]Redux-saga call multiple functions/actions

I want to call two different functions in my sagas with different actions:我想用不同的动作在我的传奇中调用两个不同的函数:

export default function* () {
  yield takeLatest(actionTypes.ADD_ACCOUNT, addAccountSaga);
  yield takeLatest(actionTypes.GET_WALLET, getBalanceSaga);
}

I've read some articles and I tried to put them in array like this :我读过一些文章,我试着把它们放在这样的数组中:

yield [(
  takeLatest(actionTypes.ADD_ACCOUNT, addAccountSaga), 
  takeLatest(actionTypes.GET_WALLET, getBalanceSaga)
)];

But sadly it did not work, I also tried this method:但遗憾的是它没有奏效,我也尝试了这种方法:

yield * takeLatest(actionTypes.ADD_ACCOUNT, addAccountSaga);
yield * takeLatest(actionTypes.GET_WALLET, getBalanceSaga);

I also putted console.log in one of the functions to be sure if its working or not but console.log did not worked which means function is not being called我还将 console.log 放在其中一个函数中以确保它是否正常工作,但 console.log 没有工作,这意味着没有调用函数

But this also did not work.但这也不起作用。 Could you help me to solve this problem?你能帮我解决这个问题吗?

You can use all effect combinator您可以使用all效果组合器

Creates an Effect description that instructs the middleware to run multiple Effects in parallel and wait for all of them to complete创建一个效果描述,指示中间件并行运行多个效果并等待所有效果完成

yield all([
  takeLatest(actionTypes.ADD_ACCOUNT, addAccountSaga), 
  takeLatest(actionTypes.GET_WALLET, getBalanceSaga)
])

for multiple in one file you have to do something like following:对于多合一文件,您必须执行以下操作:

export function* getShirtPlans() {
  try {
    const response = yield call(API.apiGetShirtPlans);
    yield put(ACTIONS.setShirtPlans(response.data.data.intervals));
  } catch (err) {
    console.log(err);
  }
}

export function* getShirtStyles() {
  try {
    const response = yield call(API.apiGetShirtStyles);
    console.log(response.data.data);
    yield put(ACTIONS.setShirtStyles(response.data.data));
  } catch (err) {
    console.log(err);
  }
}

export function* watchOngetShirtPlans() {
  yield takeLatest(TYPES.GET_SHIRTS_PLANS, getShirtPlans);
}

export function* watchOngetShirtStyles() {
  yield takeLatest(TYPES.GET_SHIRTS_STYLES, getShirtStyles);
}

and in main saga index file import like that:并在主要的 saga 索引文件中像这样导入:

import { fork, all } from "redux-saga/effects";
import { watchOngetShirtPlans, watchOngetShirtStyles } from "../sagas/ShirtsSaga";

export function* watchSagas() {
  yield all([
    fork(watchOngetShirtPlans),
    fork(watchOngetShirtStyles)
  ]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM