React Native Fetch API不返回我的调用

尼克拉斯·林德克

对不起标题中的笑话。

我目前正在使用React Native探索fetch API,但是遇到了一些我无法解决的问题。

因此,我正在尝试从服务器获取消息,该消息通过以下方式通过访存API进行调用:

var serverCommunicator = {
    test: function() {
        fetch(baseUrl  , {
          method: 'GET',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          }
        })
        .then((response) => response.text())
        .then((responseText) => {
             return (JSON.stringify(responseText));
        })
        .catch((error) => {
          console.warn(error);
        }).done();
    },
module.exports = serverCommunicator;

当我仅使用console.log(responseText)日志进行测试时,会给我正确的消息。但是,现在当我想尝试将内容作为消息放入视图中时,它并没有按预期返回。可以通过以下方式调用它:

import Method from '../Services/Methods';
.
.
.
<View style={styles.card}>
   <CardView
      message={Method.test()} 
    />

我可以看到在这样调用函数测试时如何正确调用它,但是由于某种原因,它不会编写消息。

z

这是一个经典的异步问题,您的then函数在render调用后返回,而无处可return去。

最常见的解决方案:显示空状态消息/加载指示器,并在安装组件时获取服务器信息。当您的诺言返回并且then触发了回调时,请使用您的期望值设置将触发重新渲染的组件状态。

class extends React Component

class YourComponent extends Component {
  constructor() {
    super()
    this.state.text = 'Loading, please wait!' // default text
  }

  componentDidMount() {
    fetch(baseUrl, options)
      .then((response) => response.text())
      .then((responseText) => {
         this.setState({ text: responseText }) // this triggers a re-render!
      })
  }

  render() {
    return (
      <View style={styles.card}>
        <CardView
          message={this.state.text} // <-- will change when fetch call returns
        />
      </View>
    )
  }

}

React.createClass

var YourComponent = React.createClass({
  getInitialState() {
    return { text: 'Loading, please wait!' }
  }, // <-- comma between functions, because object keys

  componentDidMount() {
    fetch(baseUrl, options)
      .then((response) => response.text())
      .then((responseText) => {
         this.setState({ text: responseText }) // this triggers a re-render!
      })
  },

  render() { /* ..same as above.. */ }
})

如果要将当前架构保留在服务中的fetch调用所在的位置,则需要将初始调用返回到fetch,这将返回promise。然后,您可以连接then您的构造函数:

var serverCommunicator = {
  test: function() {
    return fetch(baseUrl, options) // return a promise! ..important!
      .then((response) => response.text())
      .then((responseText) => {
         return responseText
      })
  }
}

然后您导入的函数将返回一个Promise。

import Method from '../Services/Methods'

...

componentDidMount() {
  Method.test().then(responseText => {
    this.setState({ text: responseText })
  })
]

  ....

希望这可以澄清有关诺言如何工作以及如何state在React中使用异步数据的知识

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Native Fetch API调用未发出请求

来自分类Dev

React Native Fetch异步

来自分类Dev

React Native fetch,意外的令牌

来自分类Dev

如何使用React Native Fetch()从API检索格式错误的json

来自分类Dev

在类方法 React Native 中包装 fetch API

来自分类Dev

如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

来自分类Dev

如何使用.fetch函数使API调用IPV6在react-native中兼容?

来自分类Dev

无法在React Native中使用`fetch()`

来自分类Dev

对 Microsoft Azure 的 React Native fetch 请求失败

来自分类Dev

.catch() 在 react-native 的 fetch 中

来自分类Dev

React Native fetch vs XMLHttpRequest 性能

来自分类Dev

在 React Native 中获取 php 文件时,如何修复 fetch 返回 404?

来自分类Dev

React Native - 如何从我的 fetch 方法中创建一个全局变量

来自分类Dev

React native fetch... 像我 5 岁一样解释它

来自分类Dev

React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

来自分类Dev

如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

来自分类Dev

react-native-fetch-blob IOS API openDocument 不起作用

来自分类Dev

从 React fetch 调用组件方法

来自分类Dev

如何在React Native中使用Fetch发布表单?

来自分类Dev

fetch()在React-native(iOS)上执行GET而不是POST

来自分类Dev

如何从React Native Fetch Blob访问下载的文件?

来自分类Dev

react-native 尝试解析内置的 fetch 函数

来自分类Dev

React Native + Fetch => TypeError:无法读取未定义的属性“then”

来自分类Dev

在 React Native fetch 方法中解析 JSON 不起作用

来自分类Dev

HTTP fetch 请求总是在 react-native 上失败

来自分类Dev

如何在 React-Native 或 Expo 的 fetch 中使用 headers

来自分类Dev

react native中的axios和fetch有什么区别?

来自分类Dev

为什么我的Fetch请求没有在React-Native中发送自定义标头?

来自分类Dev

React Native fetch 在另一个 fetch 回调中不起作用

Related 相关文章

  1. 1

    React Native Fetch API调用未发出请求

  2. 2

    React Native Fetch异步

  3. 3

    React Native fetch,意外的令牌

  4. 4

    如何使用React Native Fetch()从API检索格式错误的json

  5. 5

    在类方法 React Native 中包装 fetch API

  6. 6

    如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

  7. 7

    如何使用.fetch函数使API调用IPV6在react-native中兼容?

  8. 8

    无法在React Native中使用`fetch()`

  9. 9

    对 Microsoft Azure 的 React Native fetch 请求失败

  10. 10

    .catch() 在 react-native 的 fetch 中

  11. 11

    React Native fetch vs XMLHttpRequest 性能

  12. 12

    在 React Native 中获取 php 文件时,如何修复 fetch 返回 404?

  13. 13

    React Native - 如何从我的 fetch 方法中创建一个全局变量

  14. 14

    React native fetch... 像我 5 岁一样解释它

  15. 15

    React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

  16. 16

    如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

  17. 17

    react-native-fetch-blob IOS API openDocument 不起作用

  18. 18

    从 React fetch 调用组件方法

  19. 19

    如何在React Native中使用Fetch发布表单?

  20. 20

    fetch()在React-native(iOS)上执行GET而不是POST

  21. 21

    如何从React Native Fetch Blob访问下载的文件?

  22. 22

    react-native 尝试解析内置的 fetch 函数

  23. 23

    React Native + Fetch => TypeError:无法读取未定义的属性“then”

  24. 24

    在 React Native fetch 方法中解析 JSON 不起作用

  25. 25

    HTTP fetch 请求总是在 react-native 上失败

  26. 26

    如何在 React-Native 或 Expo 的 fetch 中使用 headers

  27. 27

    react native中的axios和fetch有什么区别?

  28. 28

    为什么我的Fetch请求没有在React-Native中发送自定义标头?

  29. 29

    React Native fetch 在另一个 fetch 回调中不起作用

热门标签

归档