如何在收到 redux 的第一个响应后调用 backed API?
How to call backed API after getting first response from redux?
以下是我的 reducer 到 redux 端。
我想在收到服务器端的响应后调用一个 axios
调用。
这里有两个函数,其中包含 getList
和 updateList
。所以我的要求是当 updateListSuccess
得到响应时,我需要调用函数 getDetails.
如何在得到用粗体字母或星号表示的响应后调用getDetails
函数。
export function updateList(_id, data) {
return dispatch => {
dispatch(updateListLoading());
axios
.put(`/api/dataList/${_id}`, data)
.then(res => {
**dispatch(updateListSuccess(res.data));**
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(updateListFail(err.response.data));
}
});
};
}
export function getDetails() {
return dispatch => {
dispatch(getDetailsLoading());
axios
.get("/api/getDetails")
.then(res => {
dispatch(getSuccessResponse(res.data));
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(getLoadfail(err.response.data));
}
});
};
}
谢谢。
好的,当你从第一次调用中获得成功时,只需调度你的其他函数
你可以这样做
export function updateList(_id, data) {
return dispatch => {
dispatch(updateListLoading());
axios
.put(`/api/dataList/${_id}`, data)
.then(res => {
dispatch(updateListSuccess(res.data));
dispatch(getDetails())
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(updateListFail(err.response.data));
}
});
};
}
希望对你有帮助
以下是我的 reducer 到 redux 端。
我想在收到服务器端的响应后调用一个 axios
调用。
这里有两个函数,其中包含 getList
和 updateList
。所以我的要求是当 updateListSuccess
得到响应时,我需要调用函数 getDetails.
如何在得到用粗体字母或星号表示的响应后调用getDetails
函数。
export function updateList(_id, data) {
return dispatch => {
dispatch(updateListLoading());
axios
.put(`/api/dataList/${_id}`, data)
.then(res => {
**dispatch(updateListSuccess(res.data));**
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(updateListFail(err.response.data));
}
});
};
}
export function getDetails() {
return dispatch => {
dispatch(getDetailsLoading());
axios
.get("/api/getDetails")
.then(res => {
dispatch(getSuccessResponse(res.data));
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(getLoadfail(err.response.data));
}
});
};
}
谢谢。
好的,当你从第一次调用中获得成功时,只需调度你的其他函数 你可以这样做
export function updateList(_id, data) {
return dispatch => {
dispatch(updateListLoading());
axios
.put(`/api/dataList/${_id}`, data)
.then(res => {
dispatch(updateListSuccess(res.data));
dispatch(getDetails())
})
.catch(err => {
if (err.response && err.response.data) {
dispatch(updateListFail(err.response.data));
}
});
};
}
希望对你有帮助