0

我想为 fetch 调用两个函数(尤其是异步等待),一个用于 POST,另一个用于 GET。我们可以在单个 useEffect 挂钩中完成吗?因此,如果我有一个自定义钩子或上下文,我可以根据 api 调用访问它。如果要调用的 api 是 GET,我必须调用 getRequest() 和 postRequest 来进行 POST。我想为此调用一个 useCustomeHook。什么是最好的方法来做到这一点。??

现有代码与此类似:

function getRequest(url,data){
    return $http({
      method : 'POST',
      url,
      data : JSON.stringify(data),
      cache : false,
      headers : {
        Accept : 'application/json',
        'Content-Type' : 'application/json',
        'charset' : 'utf-8'
      }
   });
}

function postRequest(url,data){
    return $http({
      method : 'POST',
      url,
      data : JSON.stringify(data),
      cache : false,
      headers : {
        Accept : 'application/json',
        'Content-Type' : 'application/json',
        'charset' : 'utf-8'
      }
   });
}

4

0 回答 0