React Native 和 require('http')

2024-01-31

React Native 附带的节点核心似乎不包括节点核心 http。是否可以在 React Native 中添加并使用它?

提前谢谢了。


根据 React-Native 团队的说法,

对于这种特定情况,您可能需要使用 fetch API 是环境提供的。 React Native 不在其中运行 节点运行时。

fetch工作原理类似于http。以下是如何使用它的简短示例:

// Using fetch to POST

fetch(requestURL, {
  method: 'POST',
  headers: {
   'Accept': 'application/json',
   'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    message: this.state.input,
  })
 })

// Using fetch to GET

fetch(requestURL)
  .then((response) => response.json())
  .then((responseData) => {
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(responseData),
      loaded: true,
    });
  })
   .done();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Native 和 require('http') 的相关文章

随机推荐