React Native and require('http')

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

The node core shipped with React Native does not seem to include node core http. Is it possible to add it and use at all within React Native?

Many Thanks in Advance.

回答1:

I think you're stuck at the moment. My understanding is that while React Native uses nodejs to get up and running, the runtime isn't actually nodejs, which is why you can't just require http.

This closed issue says pretty much that, with regards to util and request from nodejs:

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



回答2:

According to the react-native team,

For this specific case you'll likely want to use the fetch API which is provided by the environment. React Native does not run inside of the node runtime.

fetch works similarly to http. Here is a short example of how to use it:

// 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();


回答3:

Try this module: https://github.com/peter4k/react-native-backbone. It use backbone concept and has some http method.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!