Section 03 · 11 rules

State & data

Kind of state Home Example
Server data (the API owns it) Query library (TanStack Query by default) balance, transaction list, profile
UI state of one screen useState / useReducer modal open, selected tab
Form input in progress useState; a form library once a form has 5+ fields or cross-field rules transfer amount
Shared by one subtree Context placed at that subtree multi-step wizard
App-wide client state Context if it rarely changes; a small store (Zustand) only after the ST4 trigger theme, locale, onboarding seen
Navigation input Route params, IDs only transactionId
Persisted, not sensitive MMKV or AsyncStorage, whichever the repo uses last selected tab
Secrets Keychain / Keystore via expo-secure-store or react-native-keychain access and refresh tokens
  • ST1 NEVER copy server data into a store or useState (no setUser(data) after a fetch). Read it from the query; the cache is the single source.
  • ST2 MUST derive instead of store: totals, filtered lists and isValid are computed during render. NEVER sync state with useEffect + setState.
  • ST3 MUST keep state as low in the tree as possible and lift it only when a sibling needs it.
  • ST4 NEVER add a global store until 3+ unrelated screens read and write the same client state. Then one small store per concern, inside the feature that owns it.
  • ST5 MUST pass IDs in navigation params, never whole objects. The next screen reads the object from the query cache.
  • ST6 MUST keep query keys and fetchers in the feature’s api/ folder, one queryOptions factory per resource, never inline in screens.
  • ST7 MUST invalidate or update the affected queries after a mutation. NEVER refresh data with manual flags or counters.
  • ST8 MUST wire the query library to the device once, at the app root: online status (NetInfo or expo-network via onlineManager) and app focus (AppState via focusManager). Check the library’s React Native docs for the installed version (A5).
  • ST9 NEVER store tokens, PINs or personal data in AsyncStorage or unencrypted MMKV. Secrets go to Keychain / Keystore. Secure storage is not a source of truth: the server can always reissue.
  • ST10 NEVER use floating-point arithmetic for money. Keep amounts as integers in the smallest unit (or as the API’s strings) and format only for display.
  • ST11 MUST, in an existing repo, follow its data layer (A2). If server data lives in Redux Toolkit / RTK Query, keep it there; NEVER add a second query library beside it.
// NEVER: server data copied into state, derived value synced by effect
const [user, setUser] = useState<User>();
useEffect(() => { api.getUser(id).then(setUser); }, [id]);
const [total, setTotal] = useState(0);
useEffect(() => setTotal(items.reduce((s, i) => s + i.amount, 0)), [items]);

// MUST: read from the query, derive during render
const { data: user } = useQuery(userQueries.detail(id));
const total = items.reduce((s, i) => s + i.amount, 0);