Skip to content
groundworkv0.2

State & data fetching

Where data lives on the frontend, how it gets there, and how to keep it fresh.

7 min read · Last reviewed November 20, 2026

State & data fetching

Most frontend bugs are state bugs: stale data, duplicate sources of truth, race conditions. The fix is to decide where each piece of state lives before anything gets built.

You'll be able to#

  • Classify state and pick the right home for each kind
  • Choose between server rendering, client fetching and caching libraries
  • Recognize over-engineered state management

The mental model#

There are four kinds of state, and each has a natural home:

KindExampleHome
Server stateList of ordersThe database. On the client, it's a cache (Server Components, TanStack Query, SWR)
URL stateCurrent page, filters, selected tabThe URL (search params)
Local UI stateIs this modal open?Component state (useState)
Global client stateTheme, shopping cart before checkoutContext or a small store (Zustand)

Most apps need far less "global state" than agents give them.

Key concepts#

  • Fetching on the server vs the client: Server Components and loaders fetch before HTML is sent, which is fast and SEO-friendly. Client fetching happens after the page loads.
  • Caching & : How long fetched data is considered fresh, and how to invalidate it after a mutation.
  • Mutations: Writes (form submits, Server Actions, POST requests). After a mutation, revalidate or update the cache.
  • Optimistic updates: Show the change immediately and roll it back if the server rejects it.
  • Race conditions: An older response arriving after a newer one. Libraries handle this; hand-rolled useEffect fetching often doesn't.
  • : The client "taking over" server-rendered HTML. A hydration mismatch means the server and client rendered different output.

Decisions you make (not the agent)#

Your call, not the agent's

DecisionSensible defaultChoose otherwise when…
Server dataFetch in Server Components / loadersHighly interactive client views → TanStack Query
Filters, paginationURL search paramsEphemeral UI only
Global storeNone until it hurts; then ZustandLarge existing Redux app
WritesServer Actions / form actions + revalidateSeparate API backend → TanStack Query mutations

What good output looks like#

  • Server data is fetched on the server, or through a library that handles caching, rather than hand-rolled in effects
  • Filters, sort and pagination live in the URL, so refresh and sharing work
  • Each piece of data has one home, and nothing is copied into a second place
  • After a write, the affected data is revalidated or updated
  • Loading, error and cancellation are handled

Red flags#

  • useEffect + fetch + useState everywhere, with no error handling or cancellation
  • The same data copied into a global store and a component's state
  • Redux added to a five-page app
  • Filters that disappear on refresh because they weren't kept in the URL
  • Common mistakes: several booleans (isLoading, isError, isSuccess) that can contradict each other instead of one status; storing a copy of an object when only its ID is needed; computing a derived value in an effect and storing it in state instead of calculating it while rendering

Worked example#

Task: Add a status filter to the orders list.

You prompt: "Add a status filter to the orders page." The agent adds a useState for the filter and a useEffect that fetches orders whenever it changes.

What a good result contains: the filter value in the URL (?status=shipped), the orders fetched on the server or through a data library for that URL, and a list that always matches the current filter.

The wrong turn: if someone switches filters quickly, responses can arrive out of order, so the screen shows results for a filter that is no longer selected. React's documentation describes exactly this in effect-based fetching, and the fix it gives is a cleanup step that ignores stale responses, though it recommends the mechanisms built into frameworks. The filter also disappears when the page is refreshed, because it only exists in component state.

How review catches it: switch filters quickly with the network throttled, then refresh the page and share the URL with yourself in another tab. Both break. Ask the agent where each piece of state lives and what kind it is. Then re-prompt: filters in the URL, data fetched for that URL, no effect-based fetching.

Sample prompts#

Prompt
Refactor the orders page so that: orders are fetched on the server, filters
(status, date range) and pagination live in URL search params, and creating an
order uses a Server Action that revalidates the list. Remove the useEffect-based
fetching and the unused Zustand slice. Keep the existing UI.

Why it works: Every kind of state gets a named home, and the removal list stops old and new approaches from coexisting.

Prompt
Audit this app's state management. For each piece of state, tell me what kind it
is (server/URL/local/global), where it currently lives, and where it should live.
Flag duplicated sources of truth.

Why it works: Classifying before changing anything shows you the problems first, and "duplicated sources of truth" is the bug behind most stale-data reports.

Audit effect-based fetching#

Prompt
Find every useEffect in this codebase that fetches data. For each, tell me: whether it ignores stale responses (cleanup), how it handles errors and loading, whether the data could instead be fetched on the server or through the data library we already use, and whether the same data also lives somewhere else. Don't change anything yet.

Why it works: It turns "state is messy" into a list of specific effects with a verdict each.

Debug stale data after a save#

Prompt
After I save an order, the list still shows the old data until I refresh. Trace what happens after the write: where the list's data comes from, how it is cached, and what should refresh it. Explain the cause, then fix it by revalidating or updating that data. Show the before and after behaviour.

Why it works: Stale data after a mutation almost always means nothing told the cache to refresh. Naming that sends the agent to the right place.

Explain where each piece of state lives#

Prompt
For the change you just made, list every piece of state it uses, and for each: its kind (server, URL, local or global), where it lives, what the single source of truth is, and what happens when the user refreshes the page. Point out anything that exists in two places.

Why it works: The refresh question exposes the state that isn't where it should be. Verify the answer in the code.

Check your understanding#

Check your understanding

1/3Where should a list's filters and pagination live?
2/3A user switches filters quickly and the screen shows results for an older filter. What is this called, and how is it handled?
3/3A component has isLoading, isError and isSuccess booleans. What is the better structure?

Checklist#

  • Each piece of state is classified (server, URL, local, global) and lives in one place
  • Filters and pagination are in the URL
  • No effect-based fetching without cleanup and error handling
  • After every write, the affected data is revalidated or updated
  • Loading, empty and error states exist for fetched data

Go deeper#

Saved in this browser only.