下面的代码调度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");
答案 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"));
};
};