Server state vs UI state
Sort every piece of data in the Taskboard client into server state, a cached copy of something the database owns, or UI state, and give each the right home.
Calling the API from the clientError handling across the stack
Server state vs UI state
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Server state is a cached copy of data the server owns, so it can be out of date, shared between screens and changed by other people, and UI state is data only this browser tab owns.
- Copying fetched data into a client store with an effect creates a second source of truth that you then have to keep in sync by hand.
- A query cache keyed by the request, such as ['board', 1], deduplicates concurrent requests and lets every component that asks for the same data share one fetch.
- Invalidating a query marks it stale, and only queries that are currently being used by a mounted component refetch right away.
- Filters, selected tabs and open dialogs are UI state, and putting the ones worth sharing in the URL makes them survive reload and work with the back button.
Two kinds of data, one component tree
A Taskboard screen shows a list of tasks, a search box, a dialog for editing a task, the current user’s name, and a “saving…” spinner. They look like peers. Treat them as peers, put them all in useState or all in one global store, and the app slowly turns buggy in ways that are hard to reproduce.
The difference is ownership.
- The list of tasks is owned by the database. Other people can add to it while you look at the screen. What the browser has is a photograph of it, taken at some moment.
- The open dialog is owned by this tab. Nobody else cares, and the server never hears about it.
The first kind is server state. The second is UI state. The practical difference: server state can be wrong, because the photograph ages. UI state is never wrong, because the tab is the only authority.
The mistake: copying the photograph
The most common first version of a client looks like this:
const [tasks, setTasks] = useState<Task[]>([]);
useEffect(() => { getBoard(id).then(b => setTasks(b.tasks)); }, [id]);
It works, then it grows. You add a loading flag, an error flag, a refetch after saving, a second screen that needs the same list (so you lift the state, or put it in a global store), a guard against responses arriving out of order, a refresh on window focus. Each of these is a piece of a cache you are writing by hand. And once the list lives both in the store and on the server, two things claim to be the truth and you are responsible for reconciling them.
The core problem is that you treated a photograph as the thing itself. The better move is to admit it is a cache and use a tool built for caching. The case for a data-fetching library walks through that progression in React terms.
What a query cache gives you
A query cache stores server state under a key that describes the request: ["board", 1]. Anyone asking for that key shares the same entry. I ran TanStack Query’s core (@tanstack/react-query 5.108.2, in Node, with no React) to watch the behaviour rather than rely on memory:
const key = ["board", 1];
const [a, b] = await Promise.all([
qc.fetchQuery({ queryKey: key, queryFn: getBoard }),
qc.fetchQuery({ queryKey: key, queryFn: getBoard }),
]);
Two simultaneous requests for the same key made one call to getBoard, and both callers got the same result. That is deduplication: two components on one screen asking for the same board do not cost two round trips.
Then I called invalidateQueries({ queryKey: ["board"] }). No request went out. Reading the cache showed the entry was marked stale, but since nothing was using it, nothing refetched. Once I subscribed an observer (what a mounted component does), the fetch ran and the call count went from 1 to 2. So invalidation means “this copy is no longer trustworthy”, and the refetch happens when someone is looking. This is why invalidating after a mutation is cheap even if you name several keys.
The library also supplies what you would otherwise write: loading and error status, retries with backoff, refetch on focus, garbage collection of unused entries, and a staleTime that says how long a copy counts as fresh. TanStack Query fundamentals and caching, invalidation and mutations teach the API. The point for this lesson is the classification, not the tool.
Sorting Taskboard’s data
| Data | Kind | Home |
|---|---|---|
| Tasks on the board | Server | Query cache, key ["board", id] |
| Current user | Server | Query cache, key ["me"] |
| Task being edited (the open dialog) | UI | useState in the screen |
| Draft text in the comment box | UI | useState or the form library |
| Which column filter is active | UI, but shareable | URL search params |
| Sidebar collapsed | UI, per device | localStorage, via a small store |
| “Saving…” spinner | Derived | From the mutation’s pending status |
A few rules fall out of the table:
- Do not put server state in a UI store. If you find an effect copying query data into Redux or Zustand, stop. The cache already has it, and the copy will go stale. UI state goes in
useStatefirst and in a store only when many distant components need it (client-side state management covers the options). - Derive instead of storing. The per-status counts on the board are computed from the tasks (and in Taskboard come from the server). A
completedCountin state is a second truth that someone will forget to update. - Put shareable UI state in the URL. A column filter in the query string survives a reload, works with the back button, and can be pasted to a teammate. A filter held in
useStatecannot do any of that.
The seams to watch
Two places where the two kinds meet cause most of the bugs.
Forms. A form holds draft state (UI) that becomes a mutation to the server and later comes back as updated server state. Keep the draft separate until submit. If you initialise a form from query data and the query refetches underneath you, the user’s typing can be overwritten. Decide who wins. Forms from submit to database covers that boundary.
Optimistic updates. The UI briefly shows a result the server has not confirmed, which means temporarily editing the cache and being ready to put it back. That is the next-but-one lesson, and it only makes sense once you accept that the cache is a copy.
A last note on frameworks. Server components and loaders, covered in data loading with router loaders, move the fetch to the server for the first paint, but the browser still ends up holding a copy that can age. The classification holds either way.
What the video covers
“TanStack Query vs Zustand: What Goes Where (and How React Query Really Works)” from helo from code is about seven and a half minutes, published October 2026. Its description lists server versus client state, how the query cache works (query keys, deduplication), fresh versus stale, gcTime, mutations with invalidateQueries, and a “what goes where” sort of API data, UI state, forms, filters in the URL and a shopping cart. I could not read a transcript, so I am relying on that list. The channel is small and I only checked the title and description for English, not the spoken audio. Code & bird’s video on server state in React is linked as a second view.
Where this fits
With the data sorted, the main place a user changes server state is a form. The submit is the moment the two kinds of state meet. Next: forms from submit to database.
Check yourself
Is the list of tasks on the board server state or UI state?
Server state, because the database owns it and another person can change it, so what the browser holds is only a copy that can go stale.
Is the currently open task dialog server state or UI state?
UI state, because only this tab knows about it and the server never needs to learn it.
What happened when I asked a query client for the same key twice at the same time?
The fetch function ran once and both callers received the same result, because concurrent requests for one key are deduplicated.
Why did invalidating a query not trigger a request in my script?
Nothing was observing that query, so it was only marked stale, and it refetched once an observer subscribed.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.