Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 20 min · revision 2

Where Should This State Live?

A decision framework for placing each piece of interface state: component-local, shared client state, the URL, or the server.

Contentsand lesson path

On this page

  1. Why it matters
  2. The four homes
  3. Colocate, then derive
  4. Prop drilling: symptom, not disease
  5. Check your understanding
  6. Apply it
  7. Review prompt

Lessons of Frontend State Management

  1. Where Should This State Live? Concept · 20 min
  2. Reducers for Complex Transitions Worked example · 25 min
  3. Context Without the Re-render Tax Concept · 20 min
  4. External Stores and Selectors Worked example · 25 min
  5. Lab: Untangle a Prop-Drilled Cart Lab · 30 min
  6. Check: State Placement Scenarios Knowledge check · 6 questions

What you will learn

  • Classify application state as local UI, shared client, URL or server state, and justify where each piece should live.

Prerequisites: React Components & State

01. Why it matters

Every changing value in an interface needs a home: the text in a search box, the items in a basket, the page number of a table, the products fetched from an API. React will happily hold any of them in useState — the hard part is not how to store state but where. Choose the wrong home and you pay for it twice: once as code, because components pass values they never use through five levels of props, and once as behaviour, because a reload wipes a view your users expected to keep, or a shared link opens a different screen than the one that was copied.

Placement is also the root cause of most "we need a state management library" moments. When state lives in the wrong place, the fixes feel like new tooling. When each value lives in the right place, a small amount of plain React is usually enough — and the library, if you still want one, has a clear job description.

This lesson gives you the decision framework used by every other lesson in this skill: four kinds of state, four questions that place any value, and the two habits — colocating and deriving — that keep the result small.

02. The four homes

Client state splits along two lines: who owns the truth, and how widely it is read. That gives four homes worth naming, because each has its own tools.

Four homes for state
Home          Owner        Survives reload   Travels in links
component     the browser  no                no
shared client the browser  only if persisted no
URL           the browser  yes               yes
server cache  the server   yes (refetched)   n/a
  • Component state is data a single component owns: whether a dropdown is open, the half-typed draft in a comment box, the currently hovered row. Nobody else needs it, so nobody else should know about it. useState and useReducer live here.
  • Shared client state is owned by the browser but read in several distant places: a guest cart shown in both a header badge and a drawer, a theme, the signed-in user. Shared state is where prop drilling hurts and where reducers, context and stores (the next three lessons) come in.
  • URL state is any view parameter you would be sad to lose on refresh or when sending the link: filters, sort order, search text, the current page of a table. The browser already gives you a global, persisted, shareable store for it — the address bar — so use URLSearchParams instead of inventing a parallel one.
  • Server state is data the backend owns and your app merely borrows: products, orders, a user's saved addresses. A copy in the client is a cache: it goes stale and needs refetching, deduplication and invalidation. The fetching lesson and the query cache lab treat this kind in depth; here you only need to recognise it, because mistaking it for client state is the most expensive placement error.
Four questions that place a value
1. Who owns the truth?        component, browser, URL or server?
2. Who reads it?              one component, neighbours, or distant ones?
3. Should it survive reload,  yes: URL (or server) · no: component state
   and should links carry it?
4. How often does it change?  every keystroke needs cheap updates

The questions are ordered. Ownership decides the kind of state; readers and survival decide the home within the kind; frequency decides the mechanism — the difference between "context is fine" and "we need selectors", which lesson 4 develops.

03. Colocate, then derive

Before reaching for shared state, shrink the problem. The first habit is colocation: put each value in the lowest component that needs it, and lift it only when a second component genuinely reads it. A "quick view" panel that opens from one card should not live in the page component; an open/closed flag shared by a button and the panel it opens belongs to their common parent and no higher.

TSX · placement.tsx
import { useState } from 'react';

// Only this card reads the flag, so the flag lives here.
function ProductCard({ product }: { product: Product }) {
  const [quickViewOpen, setQuickViewOpen] = useState(false);
  return (
    <article>
      <h3>{product.name}</h3>
      <button type="button" aria-expanded={quickViewOpen}
        onClick={() => setQuickViewOpen(!quickViewOpen)}>
        {quickViewOpen ? 'Hide details' : 'Quick view'}
      </button>
      {quickViewOpen && <QuickView product={product} />}
    </article>
  );
}

The second habit is deriving: values you can compute from state you already have are not state at all. A filtered list, a total price, the count behind a badge — compute them during render from the raw data. Every copy you keep in state is a second truth that some update will eventually forget to refresh, which is exactly the drift the state and immutable updates lesson warns about. Ask of every proposed state variable: can this be calculated from what already exists? If yes, delete it and calculate it.

04. Prop drilling: symptom, not disease

Prop drilling is what happens when a value must travel through components that neither own it nor care about it: App passes cart to ShopPage, which passes it to Header, which passes it to CartBadge. Two or three levels of honest passing is fine — props are how React is meant to work, and explicit data flow is a feature. It becomes a smell when the middle components exist only to relay: their signatures carry cart, onAdd and onRemove that they never read, and any change to the cart's shape touches five files.

The important point for this lesson: drilling is a symptom of placement, not a missing library. Before adding anything, re-check the four questions. Often the value can move down (colocate), move out (derive it where it is used), move to the URL, or turn out to be server data. Only when a value is genuinely shared client state read at several distances do the tools of the next lessons — reducers, context, external stores — earn their place, and the lab will walk that refactor on a real cart.

05. Check your understanding

Practice check · 3 questions

Place the state

Three values from one shop application — give each the right home.

Q1. The app fetches a signed-in user's saved delivery addresses from the API; a settings page lists them and a checkout page picks one. How should the client treat that list?
Q2. A user picks "100 rows per page" in a data table. Nobody else on the page reads the value, links are not expected to carry it, and losing it on a reload is acceptable. Where does it belong?
Q3. A cart holds lines; a badge in the header shows the number of items and a drawer shows the total price. The developer stores `itemCount` and `totalPence` in state next to `lines`. What is the strongest criticism?
Practice only: not graded, not saved, not evidence.

06. Apply it

List ten changing values from an app you know — a work project or one of the practice projects on Tech Atlas. For each, answer the four questions in one line and name the home: component, shared client, URL or server cache. Then look at your answers as a set: which values are one bad decision away from prop drilling, and which have a derived copy hiding in state? Keep the list; the lab ends by writing exactly this justification table for a real cart, and the practice brief asks you to document each placement decision the same way.

07. Review prompt

Without looking back: name the four homes, give one example of your own for each, and recite the four questions in order. Then explain in two sentences why a filtered list should not be stored in state, and what prop drilling is a symptom of.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check