gpt4 book ai didi

react-flux 中的 Javascript 获取 API

转载 作者:行者123 更新时间:2023-11-30 16:12:40 25 4
gpt4 key购买 nike

我正在研究 flux-react-router-example @Dan Abramov,我在 Fetch API 代码中注意到 here : fetch API 有一个返回 promise ,里面有一个嵌套的返回:

return fetch(url).then(response =>
response.json().then(json => {
const camelizedJson = camelizeKeys(json);
const nextPageUrl = getNextPageUrl(response) || undefined;

return {
...normalize(camelizedJson, schema),
nextPageUrl
};
})

);

我对这个嵌套返回感到困惑,为什么在这里使用这个?为什么不直接返回 response.json()?

好像如果我做同样的事情,我会得到一个未定义的值

最佳答案

从响应中检索到的json是

  1. 使用 humps 转换为驼峰式大小写
  2. 使用 normalizr 取消嵌套与 redux store 结合使用更容易处理
  3. link header 中解析出下一页 url

有关为什么在存储中存储嵌套响应对象通常不是一个好主意的更多讨论和推理可以在 React JS Google Group 找到。 .

关于react-flux 中的 Javascript 获取 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36004933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com