Skip to the page

State, data and offline · mid

Choosing state management: server state vs client state

Separate server state (API data, best kept in TanStack Query or RTK Query with caching, dedupe and refetch) from client state (UI, session, preferences, kept in useState, Context, Zustand or Redux Toolkit), and choose the lightest tool that fits who reads the state and how often it changes.

The notes

What you must know

  • Server state is a cache of someone else's data: it goes stale and needs refetch, dedupe, retries and invalidation. TanStack Query v5 or RTK Query do this for you.
  • In React Native you must connect TanStack Query to the app: focusManager.setFocused(status === "active") from AppState, and onlineManager from NetInfo. Refetch on screen focus with useFocusEffect. Persist the cache with a persister (maxAge defaults to 24 h; keep gcTime ≥ maxAge).
  • Context re-renders every consumer when its value changes: good for theme, auth and locale, bad for fast-changing state.
  • Zustand v5: a small store with selector subscriptions. A selector that returns a new object or array needs useShallow, or it re-renders (or loops) forever.
  • Redux Toolkit 2.x: createSlice, createEntityAdapter for normalised data, createSelector for memoised selectors; extraReducers accepts only the builder callback in v2.
  • redux-persist: whitelist slices, add version + migrate, never persist tokens (use Keychain/Keystore), and remember an OTA update can change the persisted shape.
  • Optimistic updates: change the cache in onMutate, roll back on error, invalidate when settled.
  • Rule-heavy UI state (a team-selection screen, a cart) is easiest to test as a pure reducer plus small rule functions.

The red pen

Common traps: mistake → fix

  • Mistake: "Put every API response into Redux by hand"

    Fix: you rebuild caching badly; use a server-state library.

  • Mistake: "Refetch on focus works out of the box in RN"

    Fix: you must wire AppState to focusManager.

  • Mistake: "One giant Context for everything"

    Fix: every change re-renders every consumer.

Easy to hard

Questions interviewers ask, easy to hard

  1. Redux Toolkit vs Context vs Zustand vs React Query: how do you choose? Defend your current app.
  2. What is Context's performance problem?
  3. How does React Query behave when the app goes to the background and comes back?
  4. You rename a persisted slice field in v2.3; users upgrade from v2.1. What happens?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview