State Management on Mobile

Keep server state (the order list, whose original is on the server) apart from client state (the filter, form input). State used by one component: useState…

Wulan picks the "Ironing" filter, then switches to WhatsApp. If the phone needs memory, the system can quietly kill Mrs. Rini's app, and when she comes back the filter is back to "All". That filter is state: data that can change while the app runs, and that changes what's on screen when it does. In React Native it works the same as React on the web: useState, Context, or a library. What differs is the phone: the app can be left for WhatsApp at any moment, and its memory is limited. State also comes in two kinds. Say Wulan takes a photo of the order board in Mrs. Rini's shop. The photo is a copy: if Mrs. Rini moves card #13, the photo doesn't change, so she has to take it again. That's server state. Her choice "only show what's being ironed" exists on her phone alone, with no copy anywhere else. That's client state.

Server state and client state

Data Kind Kept in
The order list #12, #13, #14 server state: the original lives on Mrs. Rini's server TanStack Query
The chosen status filter client state, used by a few components useState in their parent, or Zustand
The note for the courier being typed client state, one component useState
The logged-in account client state, used on many screens Context or Zustand

Server state can go stale, needs reloading, and can fail to load. Client state changes only because of the user. Keeping the two apart makes the choice of tool clear. Until the API lesson, ORDERS is still sample data in src/data/orders.ts.

Local state and lifting state

// src/app/(tabs)/index.tsx
import { useState } from 'react';
import { FlatList, Pressable, Text, View } from 'react-native';
import { ORDERS, type Status } from '@/data/orders';

type Filter = 'All' | Status;
const OPTIONS: Filter[] = ['All', 'Queued', 'Washing', 'Ironing', 'Ready for pickup'];

function FilterChips({ active, onPick }: { active: Filter; onPick: (f: Filter) => void }) {
  return (
    <View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 8, padding: 16 }}>
      {OPTIONS.map((f) => (
        <Pressable key={f} onPress={() => onPick(f)}>
          <Text style={{ fontWeight: f === active ? '700' : '400' }}>{f}</Text>
        </Pressable>
      ))}
    </View>
  );
}

export default function Orders() {
  const [filter, setFilter] = useState<Filter>('All');
  const shown = filter === 'All' ? ORDERS : ORDERS.filter((o) => o.status === filter);

  return (
    <View style={{ flex: 1 }}>
      <FilterChips active={filter} onPick={setFilter} />
      <FlatList
        data={shown}
        keyExtractor={(o) => o.id}
        renderItem={({ item }) => <Text>#{item.id} {item.name}: {item.status}</Text>}
        ListEmptyComponent={<Text>No orders with status {filter}.</Text>}
      />
    </View>
  );
}
filter shown
'All' #12, #13, #14
'Ironing' #13 Wulan
'Queued' empty: "No orders with status Queued."

Context and re-renders

When the same data is needed on many screens (the account on the Orders tab and the Account tab), passing props through layer after layer gets tedious. Context provides it to every component under the Provider:

// src/state/orders-context.tsx: ONE context for everything
type Value = { account: string; orders: Order[]; filter: Filter; setFilter: (f: Filter) => void };
const OrdersContext = createContext<Value | null>(null);

export function OrdersProvider({ children }: { children: ReactNode }) {
  const [filter, setFilter] = useState<Filter>('All');
  return (
    <OrdersContext.Provider value={{ account: 'Wulan', orders: ORDERS, filter, setFilter }}>
      {children}
    </OrdersContext.Provider>
  );
}

// in any component under the Provider
const value = useContext(OrdersContext); // null outside the Provider

The Orders screen has four components that read this Context: Greeting, Summary, FilterChips and OrderList. Guess first: Wulan taps the "Ironing" filter. How many of the four re-render?

Every component that reads OrdersContext re-renders each time value changes, even if the part it uses stayed the same. The value object is created anew on every Provider render. What the Lab counts after one tap:

Where the state lives Tap the "Ironing" filter Server: #13 becomes "Ready for pickup"
One Context (account, orders, filter) Provider + 4 components Provider + 4 components
useState in Orders, no memo Orders + 4 components Orders + 4 components
useState in Orders, with memo Orders, FilterChips, OrderList Orders, Summary, OrderList
Zustand (filter) + TanStack Query (orders) FilterChips, OrderList Summary, OrderList

So with one Context all four re-render, including Greeting, which only reads the account name. The Expo SDK 57 template turns on the React Compiler (reactCompiler: true in app.json), which memoizes components automatically, like the third row. Context readers still re-render, though.

Zustand, Redux Toolkit, Jotai and TanStack Query

Versions on npm as of 11 October 2026: Zustand 5.0.15, Redux Toolkit 2.13.0 (with react-redux 9.3.0), Jotai 3.0.1, TanStack Query 5.104.1. Downloads in one week (2 to 8 October, every kind of project, not only React Native): TanStack Query 65.8 million, Zustand 60.8 million, Redux Toolkit 32.6 million, Jotai 6.6 million.

// src/state/filter.ts
import { create } from 'zustand';
import type { Status } from '@/data/orders';

type Filter = 'All' | Status;

export const useFilter = create<{ filter: Filter; setFilter: (filter: Filter) => void }>()((set) => ({
  filter: 'All',
  setFilter: (filter) => set({ filter }),
}));

// in a component: re-renders only when filter changes
const filter = useFilter((s) => s.filter);
// orders as server state
import { useQuery } from '@tanstack/react-query';

async function fetchOrders(): Promise<Order[]> {
  return ORDERS; // in the API lesson, this becomes a fetch to the server
}

const { data: orders = [] } = useQuery({ queryKey: ['orders'], queryFn: fetchOrders });

Background, foreground and memory

On a phone, the app isn't always in front. AppState from react-native reports 'active' (in front), 'background' (left for another app or the home screen) and 'inactive' (iOS, briefly, for example while a call comes in). Two consequences:

// src/app/_layout.tsx, inside RootLayout (QueryClientProvider already mounted)
import { focusManager } from '@tanstack/react-query';
import { useEffect } from 'react';
import { AppState } from 'react-native';

useEffect(() => {
  const sub = AppState.addEventListener('change', (status) => {
    focusManager.setFocused(status === 'active');
  });
  return () => sub.remove();
}, []);

A phone's memory is also tighter than a laptop's. Keep in global state what is really shared, not every receipt photo or a list of thousands of orders.

⚠️ Giant contexts, stale copies and lost state

TL;DR: Keep server state (the order list, whose original is on the server) apart from client state (the filter, form input). State used by one component: useState. Used by siblings: lift it to their parent (lifting state). Used on many screens: Context, but every Context reader re-renders whenever value changes (in the Lab: the Provider + 4 components for one filter tap). Zustand with selectors re-renders only the readers of the part that changed (2 components). Popular libraries: Zustand 5, Redux Toolkit 2 (structure and DevTools), Jotai 3 (atoms), and TanStack Query 5 for server state. On a phone, the app can go to the background and be killed by the system (state in memory is lost), and its memory is limited. Wire AppState to focusManager so the orders reload when the app comes back to the front.

⚡ Server state and client state

Meaning: The original order list lives on Mrs. Rini's server, so a copy can go stale: that's server state, and it belongs in TanStack Query. The "Ironing" filter Wulan picked exists on her phone alone: that's client state, and useState, Context or Zustand is enough.

⚡ Lift it to the parent

Write:

const [filter, setFilter] = useState<Filter>('All');
const shown = filter === 'All' ? ORDERS : ORDERS.filter((o) => o.status === filter);

Meaning: FilterChips and the FlatList both need filter, so the state is lifted to their parent, Orders, and flows down as props. shown is recomputed on every render; don't keep it in a second state.

⚡ One Context, everything re-renders

Guess first: four components read one Context. Wulan taps the "Ironing" filter. How many re-render?

All four, plus the Provider. Greeting too, even though it only reads the account name. With Zustand and a selector, only FilterChips and OrderList.

Meaning: Every Context reader re-renders each time value changes, even if the part it uses stayed the same.

⚡ The app can be killed

Meaning: This is what makes mobile differ from the web. While the app is in the background, the system can kill it to take back memory, and all state in memory is lost: the filter is back to "All". A phone's memory is also tighter, so save what matters to the phone's storage.

What you will learn