我想从另一个动作中发出一个动作,但无法这样做。当我尝试这样做时,它无法找到getAllUser方法。下面是我的动作课。
export const myActions = {
getAllUser() {
return (dispatch) => {
makeApiCall()
.then((response) => {
dispatch({
type: USER_SUCCESS,
payload: response,
});
})
.catch((error) => {
dispatch({
type: USER_FAILURE,
payload: error,
});
});
};
},
addUser(user) {
return (dispatch) => {
makeApiCall(user)
.then((response) => {
/*
Need help here :
wants to call above getAllUser()
.then(() =>
dispatch({
type: ADD_SUCCESS,
payload: response,
});
)
*/
};
},
};我尝试过各种各样的方法,
myActions.getAllUser()
.then((response) =>
dispatch({
type: ADD_SUCCESS,
payload: response,
});
);试着直接派遣,
const self = this;
dispatch(self.getAllUser());
dispatch({
type: ADD_SUCCESS,
payload: response,
});还有一种方法是在addUser成功之后,更新还原器,而不是再次从UI调用getAccount来刷新结果,但我很想知道如何使用多个分派来实现这一点。
发布于 2018-05-28 12:32:26
您可以单独导出函数,而不是将其封装在同一个对象下:
export const getAllUser = () => dispatch => { ... }
export const addUser = () => dispatch => {
...
dispatch(getAllUser());
}如果需要,还可以全部导入:
import * as myActions from '...';或者您可以先声明getAllUser,然后添加到myActions中,但是上面的解决方案要干净得多。
const getAllUser = ...
const myActions = {
getAllUser,
addUser = ... { dispatch(getAllUser()) }
}https://stackoverflow.com/questions/50566501
复制相似问题