Efficient React Native API Calls with Axios

🚀 React Native Tip: Clean API Calls with Axios When building scalable React Native apps, managing API calls efficiently is a must. Instead of repeating configurations everywhere, create a reusable Axios instance. 📁 Step 1: Create a central axios instance utils/axiosInstance.ts import axios from 'axios'; import { API_URL } from '@env'; const axiosInstance = axios.create({ baseURL: API_URL, // use env for flexibility headers: { 'Content-Type': 'application/json', }, timeout: 30000, withCredentials: true, }); export default axiosInstance; 📡 Step 2: Use it in your API functions const getTeamId = async (userId: string, token: string) => { try { const response = await axiosInstance.get('/tenant/v1/auth/profile', { headers: { USER_ID: userId, Authorization: `Bearer ${token}`, }, }); return response?.data?.teams; } catch (error) { console.log('Error fetching getTeamId:', error); throw error; } }; ✅ Why this approach is better: • Centralized API configuration • Easy to manage headers, base URL, and timeouts • Cleaner and reusable code #ReactNative #JavaScript #MobileDevelopment #Axios #CleanCode #CodingTips

To view or add a comment, sign in

Explore content categories