InterviewSolution
Saved Bookmarks
| 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. 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(); |
|