Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

README.md

88 lines3.0 KB

1# Offline Transactions - React Native Demo
2​
3This example demonstrates offline-first transactions using TanStack DB on React Native / Expo.
4​
5<video width="400" controls>
6 <source src="https://github.com/user-attachments/assets/c73450c6-27e1-49a7-9174-413177f5e37e" type="video/mp4">
7 Your browser does not support the video tag.
8</video>
9​
10## Features
11​
12- Offline transaction queuing with AsyncStorage
13- Automatic sync when coming back online
14- Network status detection using `@react-native-community/netinfo`
15- App state detection (foreground/background)
16​
17## Prerequisites
18​
19- Node.js 18+
20- pnpm
21- Xcode (for iOS simulator) or Android Studio (for Android emulator)
22- Expo Go app on a physical device (optional)
23​
24## Setup
25​
26From the monorepo root:
27​
28```bash
29# Install dependencies
30pnpm install
31​
32# Build the packages
33pnpm build
34```
35​
36## Running the App
37​
38```bash
39# Navigate to this example
40cd examples/react-native/offline-transactions
41​
42# Install dependencies (if not already done)
43pnpm install
44​
45# Start the backend server (in one terminal)
46npx tsx server/index.ts
47​
48# Start the Expo dev server (in another terminal)
49pnpm start
50​
51# Or run directly on iOS simulator
52pnpm ios
53​
54# Or run directly on Android emulator
55pnpm android
56```
57​
58The backend server runs on port 3001 and is shared by all emulators/devices. This allows you to test real syncing between multiple devices.
59​
60## Testing Sync Between Devices
61​
621. **Start the server** with `pnpm server`
632. **Run the app on two emulators** (e.g., iOS + Android, or two Android emulators)
643. **Add a todo on one device** - It syncs to the server
654. **Pull to refresh on the other device** - You'll see the new todo
66​
67## Testing Offline Mode
68​
691. **Add todos while online** - They sync immediately to the backend server
702. **Enable airplane mode** on your device/simulator
713. **Add more todos** - They're queued locally in AsyncStorage
724. **Disable airplane mode** - Queued transactions sync automatically
73​
74## Architecture
75​
76- **AsyncStorageAdapter**: Implements `StorageAdapter` for React Native using `@react-native-async-storage/async-storage`
77- **ReactNativeOnlineDetector**: Uses `NetInfo` and `AppState` to detect connectivity changes (imported from `@tanstack/offline-transactions/react-native`)
78- **todoCollection**: TanStack DB collection with live queries
79- **Offline actions**: Create, toggle, delete todos with optimistic updates
80​
81## Key Differences from Web
82​
83| Web | React Native |
84| -------------------------------------------- | --------------------------------------------------------- |
85| IndexedDB / localStorage | AsyncStorage |
86| `window.addEventListener('online')` | NetInfo.addEventListener |
87| `document.visibilitychange` | AppState.addEventListener |
88| Import from `@tanstack/offline-transactions` | Import from `@tanstack/offline-transactions/react-native` |