Build Mobile Apps using Codex/State and data
Lesson 1 / 5 · 16 min · 0 / 3 exercises
Course contents
Concept · 3 hands-on exercises

Local vs global state

Decide where each piece of Tipjar's state lives (a component, the URL, a server cache or a global store) and write that policy down so Codex follows it.

The most common state mistake in React Native apps isn't a bug in a library. It's putting state in the wrong place. Agents make it constantly, because “add a store” is the most popular answer in their training data. If you don't tell Codex where state belongs, it will create a global store for things that one screen owns, and copy server data into it for good measure.

Four homes for state

HomeUse it forTipjar example
Component state (useState)Values one screen or component ownsThe amount being typed into the New tip form
The URL (route params)What the screen is showing, so links and deep links workWhich tip src/app/tip/[id].tsx displays
Server cache (TanStack Query)Copies of data that lives on a serverThe list of tips from your API or Supabase
Global client store (Zustand or context)App-wide client state that unrelated screens needThe chosen currency, a has-finished-onboarding flag

The order matters. Start at the top and move down only when you have a reason. Component state is the easiest to reason about because nothing else can change it. The URL is free in Expo Router, since every screen has one, and it makes state survive a deep link. Server data is a special case: it isn't really your state at all, it's a cache of someone else's, which goes stale and needs refetching. That's TanStack Query's job, covered in lesson 3 of this module. Only what's left, client-side values that screens far apart in the tree both need, belongs in a global store.

Sort itExercise 1 of 3

Where should each piece of Tipjar's state live?

Card 1 of 5

The text typed into the history search box

What goes wrong in a store

Spot the problemsExercise 2 of 3

Codex created this Zustand store. Click the lines that put state in the wrong place.

src/lib/store.ts0 of 3 found

Notice what's left after the fix: a tiny store with one piece of genuinely global state. That's normal. Many good apps have a global store with three or four fields, or none at all. If you find yourself adding a field “in case another screen needs it,” leave it in the component until one does.

Quick checkExercise 3 of 3

The History tab and the Settings tab both show amounts in the user's currency. They don't share a parent below the root. Where should the currency live?

Global state that survives a restart

Some global state should outlive the app process, such as the chosen currency. Zustand's persist middleware writes a store to the storage you choose, such as AsyncStorage through createJSONStorage, and rehydrates it on launch. Persist only what's worth keeping: settings and flags, never copies of server data and never secrets. When you review a diff that adds persistence, check two things: which fields are saved, and what happens when a saved value no longer matches the shape the code expects after an update. The next lesson covers which on-device store fits which data.

Write the policy down

A sentence in AGENTS.md stops the drift before it starts, and costs a few dozen tokens per session. Something like: “Prefer component state. Read server data with TanStack Query; never copy it into a store. The Zustand store in src/lib/store.ts is only for app-wide client settings. Put what a screen shows in its route params.” When Codex proposes a new store field, it now has a rule to check the proposal against, and so do you when you review the diff.

Check your understanding
Q1. When does a global store actually pay off?
Q2. Why shouldn't server data be copied into a Zustand store?
· Work through the 3 remaining exercises above (stuck? each one can show you the answer).
· Score 100% on the quiz.