使用 redux 处理 reduce 中的承诺
Handling promises in the reduces using redux
我正在使用 React + redux 构建网络应用程序
我有一个数据 table 并且用户可以从 table
中删除行
为了避免用户不得不等到数据从服务器(更准确地说是数据库)中删除,我想采用面向成功的方法,这意味着从状态中删除项目并还发送 HTTP 请求以从数据库中删除此项。
我都想从reducer中执行
问题是我应该在哪里处理已解决或已拒绝的承诺?
万一它被拒绝了我也想通知用户。
谢谢
where should I handle resolved or rejected promise? In case it was rejected I would also like to notify the user?
应该在通过 reducers 进行 API 调用的函数中完成。
//减速器
const deleteRecord = (state="somestate")=>{
fnAPICall()
}
function fnAPICall(){
fetch('some_url').then(function(response) {
if(response.ok) {
dispatch({"type":"DELETE_RECORD",record_id : some_id});//actual delete from memory
return response.json();
}
dispatch({"type":"DELETED_FAILED",record_id : some_id}); //Record deleting failed.notify user
})
}
我正在使用 React + redux 构建网络应用程序
我有一个数据 table 并且用户可以从 table
中删除行为了避免用户不得不等到数据从服务器(更准确地说是数据库)中删除,我想采用面向成功的方法,这意味着从状态中删除项目并还发送 HTTP 请求以从数据库中删除此项。
我都想从reducer中执行
问题是我应该在哪里处理已解决或已拒绝的承诺? 万一它被拒绝了我也想通知用户。
谢谢
where should I handle resolved or rejected promise? In case it was rejected I would also like to notify the user?
应该在通过 reducers 进行 API 调用的函数中完成。
//减速器
const deleteRecord = (state="somestate")=>{
fnAPICall()
}
function fnAPICall(){
fetch('some_url').then(function(response) {
if(response.ok) {
dispatch({"type":"DELETE_RECORD",record_id : some_id});//actual delete from memory
return response.json();
}
dispatch({"type":"DELETED_FAILED",record_id : some_id}); //Record deleting failed.notify user
})
}