React Native Blueprints
上QQ阅读APP看书,第一时间看更新

Networking in React Native

Most mobile apps need to fetch and update data from an external URL. There are several npm modules, which can be used in React Native to communicate and download remote resources such as Axios or SuperAgent. If you are familiar with a specific HTTP library, you can use it in your React Native projects (as long as is not dependent on any browser specific APIs), although a safe and proficient option is to use Fetch, the built-in networking library in React Native. 

Fetch is very similar to XMLHttpRequest, so it will feel familiar to any web developers who had to perform AJAX requests from the browser. On top of that, Fetch supports promises and the ES2017 async/await syntax. 

The full documentation for the Fetch API can be found on Mozilla Developer Networks website https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API.

By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http instead of https), you will first need to add an App Transport Security ( ATS) exception. If you know ahead of time what domains you will need access to, it is more secure to add exceptions just for those domains; if the domains are not known until runtime, you can disable ATS completely. Note, however, that from January 2017, Apple's App Store review will require reasonable justification for disabling ATS. See Apple's documentation for more information.