Challenge your understanding of networking in React Native with this quiz focused on Fetch and Axios for making HTTP requests, handling responses, and managing network errors. Ideal for anyone looking to reinforce key concepts for robust mobile app development using modern data fetching methods.
Which method below correctly demonstrates how to make a GET request using the Fetch API in React Native?
Explanation: The Fetch API's standard syntax for a GET request starts with fetch followed by the URL in parentheses. The other options are incorrect because 'get' is not a built-in function, 'axiosFetch' is not a valid method, and 'request' is not native in React Native for HTTP requests.
When using the Fetch API without specifying a method, such as fetch('https://api/data'), what HTTP method is used by default?
Explanation: When the method property is omitted in a Fetch request, the default HTTP method used is GET, which retrieves data. POST, PUT, and DELETE must be explicitly specified when making requests that modify or remove data.
In React Native, after making a Fetch request, how do you convert the response to JSON for further use?
Explanation: The correct method to parse a response as JSON is response.json(), which returns a promise resolving with the parsed data. The other options, such as toJSON(), asJson(), and parseJSON(), do not exist in the standard Fetch API response object.
What does a basic Axios GET request, such as axios.get('https://api/user'), return in React Native?
Explanation: Axios returns a Promise that, when resolved, provides a response object containing the server's reply. It does not return an object immediately or a plain JSON string, nor does it simply yield a boolean for success.
When customizing a Fetch request in React Native, which property is used to specify additional request headers?
Explanation: The 'headers' property allows you to add or override HTTP headers in Fetch requests, such as authentication or content type. 'data' and 'params' are not valid in Fetch's options, and 'cookies' is not a property for setting HTTP headers.
Which approach best describes how to handle errors from Fetch or Axios in React Native?
Explanation: Chaining .catch() after your request ensures you process errors from the Promise, such as network issues. Using only .then() will not handle errors, as .then() expects a resolved promise. There is no tryParse() for error handling, and ignoring errors can cause the app to crash or fail silently.
When sending data using Axios POST in React Native, which argument is used for the payload?
Explanation: Axios POST requests require the data payload as the second argument after the URL. Simply providing only the URL or modifying the method property does not send data. Attaching data to the URL is not correct for POST requests.
Which statement correctly describes the use of async/await with the Fetch API in React Native?
Explanation: Async/await is compatible with the Fetch API, letting you write asynchronous code that looks synchronous by waiting for promises to resolve. Saying it is unavailable for Fetch is inaccurate, and await does not convert promises to strings. Async functions also work beyond Axios.
After an Axios request resolves, how do you access the main response data payload in React Native?
Explanation: In Axios, the primary response data is stored in the 'data' property of the response object. The options 'json', 'body', and 'content' either do not exist or are not the standard for accessing the returned data when using Axios.
Which statement correctly applies to setting a request timeout when using Axios in React Native?
Explanation: In Axios, you can set a timeout by adding a 'timeout' property to the config object passed as an argument. Fetch does not have a built-in timeout, and Axios does not automatically apply a 1-second timeout. Claiming there is no way to set timeouts is incorrect.