~/swaraj.dev
Back to all posts
February 28, 20262 min read

Building Offline-First React Native Apps

Real-world mobile users lose connectivity constantly. Here is how to architect a React Native app that works seamlessly offline.

react-nativemobileofflinearchitecture

Why Offline-First Matters

Mobile users constantly face spotty connections � tunnels, elevators, rural areas. An app that freezes or shows blank screens on disconnect feels broken. Offline-first means your app works without a connection and syncs intelligently when reconnected.

Key Strategies

1. Local Database with WatermelonDB or SQLite

Store app data locally first, sync to the server in the background.

import { Database } from '@nozbe/watermelondb';
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';

const adapter = new SQLiteAdapter({ schema });
export const database = new Database({ adapter, modelClasses });

2. React Query with Persistence

import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister';
import AsyncStorage from '@react-native-async-storage/async-storage';

const persister = createAsyncStoragePersister({ storage: AsyncStorage });

Cached data loads instantly from storage while fresh data fetches in the background.

3. Detect Network State

import NetInfo from '@react-native-community/netinfo';

NetInfo.addEventListener((state) => {
  if (state.isConnected) {
    syncPendingChanges();
  }
});

4. Optimistic Updates

Update the UI immediately, roll back on error. Use React Query mutations with onMutate / onError handlers for clean rollback logic.

Conclusion

Offline-first is a UX superpower. Start with React Query persistence for most apps, graduate to WatermelonDB when you need relational local data with complex queries.