reducer redux-thunk不调用reducer

时间:2019-02-06 05:17:52

标签: reactjs redux react-redux redux-thunk

下面的代码调度create_event,然后调用addFlashMessage操作

export const createEvent = event => {
  return dispatch => {
    dispatch({
      type: "CREATE_EVENT",
      payload: {
        event
      }
    });
    addFlashMessage("Event has been created");
  };
};

在操作时调用addFlashMessage
但是 ADD_FLASH_MESSAGE 没有被调用

export const addFlashMessage = message => {
  console.log("action is invoked");
  return dispatch => {
    dispatch({
      type: "ADD_FLASH_MESSAGE",
      payload: {
        message
      }
    });
  };
};

这永远不会被称为

 case "ADD_FLASH_MESSAGE":
            console.log("action called");

2 个答案:

答案 0 :(得分:1)

您需要dispatch action来调用addFlashMessage,如下所示。

dispatch({type: "ADD_FLASH_MESSAGE",payload:"Event has been created"});

您的代码应类似于followng。

export const createEvent = event => {
  return dispatch => {
    dispatch({
      type: "CREATE_EVENT",
      payload: {
        event
      }
    });
   dispatch({type: "ADD_FLASH_MESSAGE",payload:"Event has been created"});
  };
};

答案 1 :(得分:1)

您不仅需要createEvent来触发dispatch函数,还需要从redux-thunk调用另一个函数。

这是更正的。

export const createEvent = event => {
  return dispatch => {
    dispatch({
      type: "CREATE_EVENT",
      payload: {
        event
      }
    });
    dispatch(addFlashMessage("Event has been created"));
  };
};