react-native提取中的'then(res => res.json())'是什么意思? [英] What does 'then(res => res.json())' in react-native fetch mean?

查看:155
本文介绍了react-native提取中的'then(res => res.json())'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在本机获取中的then(res => res.json())是什么意思?

What is meant by then(res => res.json()) in the snippet below in react-native fetch?

fetch(url)
      .then(res => res.json())
      .then(res => {
        this.setState({
          data: res,
          error: res.error || null,
          loading: false
        });

推荐答案

这并不是真正的反应问题,因为fetch本身就是js的一部分.

That's not really a react question since fetch and then are parts of js itself.

fetch会以Promise的形式返回对象,其中包含各种信息,例如标头,HTTP状态等.

fetch returns an object as Promise that contains various information like headers, HTTP status etc. etc.

您有res.json()和其他各种可能性. .json()将只返回带有json内容的promise.

You have res.json() and various other possibilities. .json() will just return the body as promise with json content.

有关更多信息: https://developer.mozilla. org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

您可以按以下方式返回数据:

You can return the data as following:

  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()
  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()

这篇关于react-native提取中的'then(res => res.json())'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆