Javascript 雷杜和承诺进展
我如何处理和保证redux的进展 我想在promise执行时显示一些旋转条或其他东西,我使用它来处理请求,但它们有一个api来处理请求的config对象中的进度: 但我只能在redux操作中发送请求,如:Javascript 雷杜和承诺进展,javascript,redux,redux-thunk,Javascript,Redux,Redux Thunk,我如何处理和保证redux的进展 我想在promise执行时显示一些旋转条或其他东西,我使用它来处理请求,但它们有一个api来处理请求的config对象中的进度: 但我只能在redux操作中发送请求,如: return { type: "HTTP_REQUEST", payload: axios.get("/webAPI/data", configObj) } 在这些条件下,我如何处理进度事件 如果只显示微调器而不是进度条,则不需要进度函数。相反,我会推荐以下几点: cons
return {
type: "HTTP_REQUEST",
payload: axios.get("/webAPI/data", configObj)
}
在这些条件下,我如何处理进度事件 如果只显示微调器而不是进度条,则不需要进度函数。相反,我会推荐以下几点:
const axiosAction = function(configObj) {
// We have to thunk the dispatch since this is async - need to use the thunk middleware for this type of construct
return dispatch => {
/* action to notify the spinner to start (ie, update your store to have a
loading property set to true that will presentationally start the spinner) */
dispatch({
type: 'AXIOS_REQUEST_STARTING'
});
return axios.get("/webAPI/data", configObj)
.then(res => {
/* action to set loading to false to stop the spinner, and do something with the res */
return dispatch({
type: 'AXIOS_REQUEST_FINISHED',
payload: res,
})
})
.catch(err => /* some error handling*/);
};
}
编辑以添加链接,用于如果您只想显示微调器而不是进度条,则实际上不需要进度功能。相反,我会推荐以下几点:
const axiosAction = function(configObj) {
// We have to thunk the dispatch since this is async - need to use the thunk middleware for this type of construct
return dispatch => {
/* action to notify the spinner to start (ie, update your store to have a
loading property set to true that will presentationally start the spinner) */
dispatch({
type: 'AXIOS_REQUEST_STARTING'
});
return axios.get("/webAPI/data", configObj)
.then(res => {
/* action to set loading to false to stop the spinner, and do something with the res */
return dispatch({
type: 'AXIOS_REQUEST_FINISHED',
payload: res,
})
})
.catch(err => /* some error handling*/);
};
}
编辑以添加链接虽然Gabdalah的答案是正确的,但我觉得它只部分回答了这个问题。如果您愿意,两个答案中的代码可以轻松组合 如果确实希望向用户显示进度,则可以从进度回调中调度特定的进度操作,并将当前进度作为有效负载。大概是这样的:
{
progress: function(progressEvent) {
return dispatch({
type: "HTTP_REQUEST_PROGRESS",
payload: {
url: "/webAPI/data",
currentBytes: progressEvent.current,
totalBytes: progressEvent.total // properties on progressEvent made up by yours truly
}
});
}
}
本质上,你只需要另一个表示请求进度的动作,就像你已经有了一个发起请求的动作(可能是一个成功的和不成功的结果)。虽然Gabdalah的答案是正确的,但我觉得它只是部分回答了这个问题。如果您愿意,两个答案中的代码可以轻松组合 如果确实希望向用户显示进度,则可以从进度回调中调度特定的进度操作,并将当前进度作为有效负载。大概是这样的:
{
progress: function(progressEvent) {
return dispatch({
type: "HTTP_REQUEST_PROGRESS",
payload: {
url: "/webAPI/data",
currentBytes: progressEvent.current,
totalBytes: progressEvent.total // properties on progressEvent made up by yours truly
}
});
}
}
本质上,您只需要另一个表示请求进度的操作,就像您已经有一个用于启动请求的操作一样(可能还有一个用于成功和不成功结果的操作)