1. How do you install Axios in a React Native project?
Axios is a third-party HTTP client that simplifies network requests. It must be installed separately using npm or yarn before importing into components.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
React Native · Question Set
Networking & API Integration interview questions for placements and exams.
Questions
14
Included in this set
Subject
React Native
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for React Native.
Axios is a third-party HTTP client that simplifies network requests. It must be installed separately using npm or yarn before importing into components.
For complete preparation, combine this set with full subject-wise practice for React Native. You can also explore other subjects and sets from the links below.
The Fetch API is a built-in JavaScript method for making network requests. It returns a promise and allows handling of JSON data easily using async / await or then / catch.
fetch('https://api.example.com/data').then(res => res.json());Errors in async functions are caught using try / catch blocks. This ensures that failed API calls or JSON parsing errors don’t crash the app and can be handled gracefully.
try { const res = await fetch(url); } catch(e) { console.error(e); }Axios automatically converts JSON responses and provides request / response interceptors. Fetch requires manual response.json() calls and additional logic for interceptors.
The 'ok' property of the Fetch API response object is true for status codes 200–299. It’s a quick way to confirm whether the request was successful before reading the body.
if (!response.ok) throw new Error('Network error');Axios supports request timeouts through the timeout configuration option. Fetch does not have native timeout handling; developers must use AbortController instead.
axios.get(url, { timeout: 5000 });You can use libraries like react-query or AsyncStorage to cache responses locally. Caching reduces network calls and improves performance by serving previously fetched data instantly.
Maintain three pieces of state — loading, data, and error. Start with loading = true, then update data or error once the request resolves or fails. This pattern helps render conditional UI feedback for users.
Create an Axios instance using axios.create() with baseURL and common headers. Import this instance across files to ensure all requests share consistent configurations like interceptors and tokens.
const api = axios.create({ baseURL: 'https://api.app.com', headers: { 'Auth': token } });Use FormData to append file objects and send them via POST requests. Make sure headers include 'Content-Type': 'multipart/form-data'. Axios handles boundary setup automatically, while Fetch requires manual setup.
Avoid hard-coding API URLs and keys inside source files. Use environment variables, handle timeouts, validate responses, and cache frequent requests. Always clean up subscriptions to prevent memory leaks.
async / await helps structure asynchronous code in a linear, readable way. It improves error handling using try / catch and avoids deeply nested then / catch chains.
const res = await fetch(url); const data = await res.json();
Headers can be sent with fetch by including them in the options parameter. This is useful for authentication tokens or content-type declarations.
fetch(url, { headers: { 'Authorization': 'Bearer token' } });AbortController allows cancellation of an ongoing fetch call to free network resources. It’s often used when users leave a screen before a response arrives.
const controller = new AbortController(); fetch(url, { signal: controller.signal }); controller.abort();