Lesson 5 of 9 · 24m
State Management and Data Flow on Mobile
Design state boundaries for mobile lifecycles: server cache, local persistence, ephemeral UI state, and conflict-prone offline truth.
React Native state bugs look random because mobile state has a hostile lifecycle. The app pauses, resumes, loses network, gets killed, restores in the wrong route, and receives stale responses in a new account context. The solution is not picking the trendiest store library. It is deciding which layer owns each kind of truth and making transitions explicit.
Four kinds of state, four different owners
Do not put all application data in one global object:
server cache remote entities, freshness, retries, invalidation
local persistence drafts, settings, offline queue, cached session metadata
UI state modals, selection, input focus, transient validation
navigation state stack, tab, deep-link route, restored positionLibraries can help, but ownership is architectural. React Query or a similar server-cache layer is good at remote state. SQLite, MMKV, AsyncStorage, or secure storage solve different persistence needs. None of them absolves you from modeling lifecycle.
Mobile persistence has trust boundaries
AsyncStorage is simple but not a database and not a secrets vault. Use secure storage for tokens, SQLite or structured stores for queryable data, and file storage for large blobs:
import * as SecureStore from 'expo-secure-store'
await SecureStore.setItemAsync('session.refreshToken', refreshToken, {
keychainAccessible: SecureStore.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
})Treat persistence as versioned data. Add schema versions and migration paths because users upgrade irregularly and skip releases.
Request identity must survive navigation
A response is valid only for the state that requested it. Guard account, tenant, and request generation when asynchronous work finishes:
const generation = ++requestGeneration.current
const result = await api.fetchOrders(account.id)
if (generation !== requestGeneration.current || account.id !== activeAccountId) return
setOrders(result)Without this, a slow logout response can repopulate the screen after account switch. Mobile networks make this common, not exotic.
Offline is a product decision before it is a sync library
Offline support means answering hard questions: which records are readable offline, which writes are queued, how conflicts resolve, and what expires. A queue entry should carry intent, an entity ID, an idempotency key, a creation timestamp, and enough payload integrity information to retry safely. Server timestamps and idempotency keys matter because retries are inevitable.
State restoration is not free correctness either. After process death, the user may land deep in a flow whose parent state no longer exists. Decide whether to reconstruct, restart, or safely exit the flow. Never blindly rehydrate privileged screens after credentials or entitlements may have changed.
Field notes
- AsyncStorage writes are asynchronous; treat them as persistence operations, not synchronous memory.
- Logout must cancel or invalidate in-flight requests and clear account-scoped caches in the right order.
- Persisted Redux state without migration discipline becomes corrupted application truth across upgrades.
- Global state is expensive because every subscriber becomes a coupling point. Keep it narrow.
Was this lesson helpful?