首页 > 解决方案 > 在 react-redux 中运行两次的动作

问题描述

我正在使用 React 构建一个待办事项应用程序。在从 Firestore DB 请求/响应之后,我正在向减速器发送一个动作(COMPLETED_TASK)。由于我也在使用 redux-logger,因此我注意到此操作运行了两次。

Chrome 开发工具的屏幕截图

这是动作的代码:

export const completedTask = (data, completedBool) => async (
  dispatch,
  getState
) => {
  try {
    const formObj = cloneDeep(data);
    const onlyDate = formObj.taskTime;
    const getId = formObj._id;
    const getDate = cleanDate(onlyDate, 'DD');
    const getMonth = cleanDate(onlyDate, 'MM');
    const getYear = cleanDate(onlyDate, 'YYYY');
    const { uid } = getState().firebase.auth;

    const todoUpdateString = `todoListByDate.${getDate}.${getId}.completed`;

    await db.doc(`todos-col/${uid}&${getMonth}${getYear}`).update({
      [todoUpdateString]: completedBool,
    });

    formObj.completed = completedBool;

    dispatch({
      type: todoTypes.COMPLETED_TASK,
      payload: {
        todosByMonthData: {
          [`${getMonth}-${getYear}`]: {
            [getDate]: { [getId]: formObj },
          },
        },
        selectedDate: {
          selectedDay: getDate,
          selectedMonth: getMonth,
          selectedYear: getYear,
        },
      },
    });
  } catch (err) {
    console.log('Error!!', err);
  }
};

在屏幕截图中,在 1 处,动作被分派给减速器(它记录来自下面减速器的有效负载数据),然后再次调度类型为:“COMPLETED_TASK”的动作,但减速器没有接收到它。

为什么会这样?有人可以帮忙吗?

标签: javascriptreactjsreduxgoogle-cloud-firestorereact-redux

解决方案


推荐阅读