1.

Describing Networking in React Native and how to make AJAX network calls in React Native?

Answer»

React Native provides the Fetch API for networking needs. 
To fetch content from an arbitrary URL, we can pass the URL to fetch:

fetch('https://mywebsite.com/endpoint/', { METHOD: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ firstParam: 'yourValue', secondParam: 'yourOtherValue' })});

Networking is an inherently ASYNCHRONOUS operation. Fetch methods will return a Promise that makes it straightforward to write CODE that works in an asynchronous manner:

const getMoviesFromApi = () => { return fetch('https://reactnative.dev/movies.json') .then((response) => response.json()) .then((json) => { return json.movies; }) .catch((error) => { console.error(error); });};

The XMLHttpRequest API is built in to React Native  Since frisbee and Axios use XMLHttpRequest we can even use these libraries.

var request = NEW XMLHttpRequest();request.onreadystatechange = (e) => { if (request.readyState !== 4) { return; } if (request.status === 200) { console.log('success', request.responseText); } else { console.warn('error'); }};request.open('GET', 'https://mywebsite.com/endpoint/');request.send();


Discussion

No Comment Found