首页 > 解决方案 > 使用一批 react-redux 调度多个异步操作

问题描述

我可以对这些操作使用批处理吗?

import { batch } from 'react-redux';

export const getAAction = () => async (dispatch) => {

      const response = await getAService();
      if (response.ok) {
        dispatch({
          type: constants.GET_A,
          payload: response?.data,
        });
      }
    };

export const getBAction = () => async (dispatch) => {
  const response = await getBService();
  if (response.ok) {
    dispatch({
      type: constants.GET_B,
      payload: response?.data,
    });
  }
};

我希望我的组件一旦重新渲染,但 Action B 在 Action A 之后等待并运行,所以我可以使用它来代替 Promise.all();

 batch(async () => {
     await dispatch(getAAction());
     await dispatch(getBAction());
    });

标签: javascriptreactjsreduxreact-reduxnext.js

解决方案


推荐阅读