React Native和require('http')

Not*_*DMC 7 react-native

React Native附带的节点核心似乎不包含节点核心http.是否有可能在React Native中添加它并使用它?

提前谢谢了.

Jon*_*ang 17

根据反应原生团队的说法,

对于这种特定情况,您可能希望使用环境提供的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();
Run Code Online (Sandbox Code Playgroud)


Col*_*say 7

我觉得你现在卡住了.我的理解是,虽然React Native使用nodejs启动并运行,但运行时实际上并不是nodejs,这就是为什么你不能只是requirehttp.

这个封闭的问题几乎就是说,关于nodejs utilrequest来自nodejs:

https://github.com/facebook/react-native/issues/375