使用一批 react-redux 分派多个异步操作

时间:2021-07-07 08:52:17

标签: javascript reactjs redux react-redux next.js

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

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,
    });
  }
};

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

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

0 个答案:

没有答案