Build Mobile Apps using Codex/State and data
Lesson 3 / 5 · 18 min · 0 / 2 exercises
Course contents
Sandbox demo · 2 hands-on exercises

Connecting to a REST API

Fetch server data with TanStack Query: caching, pull-to-refresh, refetching on foreground and reconnect, and honest loading, empty and error states.

fetch works fine in React Native. The hard part is everything around it: not refetching on every visit, retrying when the network blips, refreshing when the user comes back to the app, and showing something sensible while loading or after a failure. TanStack Query (v5, version 5.107 as of November 2026) handles most of that, but on mobile you have to connect two of its triggers yourself.

Fresh, stale and garbage-collected

TanStack Query keeps a cache keyed by queryKey. Two timers control it. staleTime is how long data counts as fresh: within that window, visiting the screen again reads the cache with no request. gcTime (called cacheTime before v5) is how long unused data stays in memory before it's dropped. On a phone, a staleTime of 30 to 60 seconds for lists like Tipjar's history saves a request on almost every back-and-forth navigation, which is battery and data the user doesn't spend.

Quick checkExercise 1 of 2

History uses staleTime: 60_000. The user opens History, goes to Settings, and comes back 20 seconds later. What happens?

Two mobile triggers to wire up

On the web, TanStack Query refetches stale data when the browser tab regains focus and when the browser comes back online. React Native has no browser events, so you connect them: point focusManager at React Native's AppState, so returning to the app counts as focus, and point onlineManager at a network status library such as @react-native-community/netinfo. Then pull-to-refresh covers the case where the user explicitly asks for new data.

Spot the problemsExercise 2 of 2

Codex wrote this from an older tutorial. Click the lines that won't work, or won't work on a phone.

src/app/(tabs)/history.tsx (excerpt)0 of 3 found

Design all the states

Every screen that loads data has at least four states: loading for the first time, showing data, empty, and failed. Offline adds a fifth, showing cached data with a note that it may be out of date. Agents tend to build the happy path and a spinner. Name the others in your prompt, and say what the error state should offer, usually a Retry button and a short human message rather than the raw error. Expo's expo-data-fetching skill covers these states, so with the Expo plugin installed, Codex has guidance to follow.

Sandbox transcript
Codex · sandbox replay
Press Play to replay this transcript step-by-step.
0 / 8

The transcript is canned — no real API calls. The point is to internalize the loop: user → plan → tool → result → answer.

Check your understanding
Q1. Why is fetching on every mount a problem on a phone?
Q2. In TanStack Query v5, what replaced the cacheTime option?
· Work through the 2 remaining exercises above (stuck? each one can show you the answer).
· Score 100% on the quiz.