Prompt
How do I integrate a mobile backend with React Native?
Latest observation
Integrating a mobile backend with React Native usually means:
- Expose your backend through an API
- Call that API from React Native
- Handle auth, data, errors, and offline/state concerns
Here’s a practical way to do it.
1) Choose how your backend is exposed
Most React Native apps talk to backend services via one of these:
- REST API – most common, simple, easy to debug
- GraphQL – good for flexible data fetching
- Firebase/Supabase/Appwrite – backend-as-a-service options
- WebSockets / real-time APIs – for chat, live updates, etc.
If you already have a backend, REST is often the easiest integration path.
2) Make sure your backend is mobile-friendly
Your backend should support:
- HTTPS
- JSON request/response
- Authentication (JWT, OAuth, session tokens, etc.)
- Pagination for list endpoints
- Consistent error responses
Example REST endpoint:
GET https://api.example.com/profile
Authorization: Bearer <token>
Response:
{
"id": 1,
"name": "Jane Doe",
"email": "jane@example.com"
}
3) Install a networking library in React Native
You can use built-in fetch, or a more powerful library like axios.
Using fetch
No extra install needed.
Using axios
npm install axios
4) Create a reusable API client
It’s best not to call fetch all over your app. Create one service file.
Example with axios
// api/client.js
import axios from 'axios';
const api = axios.create({
baseURL: 'https://api.example.com',
timeout: 10000,
});
api.interceptors.request.use(async (config) => {
const token = 'YOUR_AUTH_TOKEN'; // replace with secure storage later
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
export default api;
5) Call backend endpoints from components or hooks
// screens/ProfileScreen.js
import React, { useEffect, useState } from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
import api from '../api/client';
export default function ProfileScreen() {
const [profile, setProfile] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function loadProfile() {
try {
const response = await api.get('/profile');
setProfile(response.data);
} catch (error) {
console.error('Failed to fetch profile:', error);
} finally {
setLoading(false);
}
}
loadProfile();
}, []);
if (loading) return <ActivityIndicator />;
return (
<View>
<Text>{profile?.name}</Text>
<Text>{profile?.email}</Text>
</View>
);
}
6) Handle authentication securely
Never hardcode tokens in production.
Use secure storage:
- Expo:
expo-secure-store - Bare React Native:
react-native-keychainorreact-native-encrypted-storage
Example:
npm install react-native-encrypted-storage
Then store and retrieve tokens safely.
7) Manage backend state cleanly
If your app fetches lots of server data, consider:
- React Query (
@tanstack/react-query) for caching and syncing - Redux Toolkit Query
- SWR
These help with:
- caching
- retries
- refetching
- loading/error states
Example with React Query
npm install @tanstack/react-query
import { useQuery } from '@tanstack/react-query';
import api from '../api/client';
function useProfile() {
return useQuery({
queryKey: ['profile'],
queryFn: async () => {
const res = await api.get('/profile');
return res.data;
},
});
}
8) Configure your backend for mobile development
Android emulator
If your backend runs on your local machine:
localhostfrom Android emulator usually won’t work as expected- Use:
10.0.2.2for Android emulator- your machine’s LAN IP for physical devices
Example:
baseURL: 'http://10.0.2.2:3000'
iOS simulator
localhost usually works on iOS simulator if backend is local.
Physical device
Use your machine’s local IP:
baseURL: 'http://192.168.1.20:3000'
9) Watch out for CORS? Usually not on mobile
CORS is mainly a browser issue. React Native is not a browser, so CORS is usually not a problem in the same way.
But:
- your backend still needs proper auth
- HTTPS certificates should be valid in production
10) Common best practices
- Use environment variables for API URLs
- Add timeouts and retries
- Show loading and error states
- Use secure token storage
- Keep API logic in a separate layer
- Use TypeScript if possible for stronger typing
- Log errors to tools like Sentry or Firebase Crashlytics
11) Example architecture
A clean structure might look like:
src/
api/
client.js
auth.js
users.js
hooks/
useProfile.js
screens/
ProfileScreen.js
components/
12) If your backend is Firebase/Supabase
You usually don’t build your own API layer in the same way. Instead, you:
- install the SDK
- initialize the client
- call auth/database/storage methods directly
For example, with Supabase:
- sign in with
supabase.auth.signInWithPassword() - fetch data from tables
- subscribe to real-time updates if needed
Quick recommendation
If you’re starting from scratch:
- Use REST API
- Use axios
- Use React Query
- Store auth tokens in secure storage
- Keep backend URLs in environment config
If you want, I can also give you:
- a full React Native + Node/Express example
- a React Native + Firebase example
- a production-ready API folder structure