【问题标题】:Handling promises in the reduces using redux使用redux处理reduce中的promise
【发布时间】:2017-09-05 07:37:24
【问题描述】:

我正在使用 react + redux 构建一个网络应用

我有一个数据表,用户可以从表中删除行

为了避免用户不得不等到数据从服务器(更准确地说是数据库)中删除,我想采用面向成功的方法,这意味着从状态中删除项目并发送从数据库中删除此项的 HTTP 请求。

我都想从 reducer 执行

问题是我应该在哪里处理已解决或拒绝的承诺? 如果它被拒绝,我也想通知用户。

谢谢

【问题讨论】:

    标签: reactjs promise xmlhttprequest react-redux reduce


    【解决方案1】:

    我应该在哪里处理已解决或拒绝的承诺?如果被拒绝了,我还要通知用户吗?

    应该在通过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
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2017-08-27
      • 1970-01-01
      • 2019-01-03
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多