Sub topic 9
Deciding where state lives
This chapter assumes you know useState, props, context, and the feature-module layout from the earlier chapters. The goal is a repeatable way to choose where each piece of state belongs.
Start low, move up only when forced
State should live in the lowest place that still lets every consumer reach it. Moving it up has a cost: more components re-render, and more code depends on it. Use this order:
- Component state. Only one component reads it. Examples are an open dropdown or an input draft.
- Lifted state. Two siblings need it, so move it to their closest common parent.
- Feature state. Many components in one feature need it. Keep it in a feature hook or a feature-scoped context.
- Shared state. Several features need it, such as the signed-in user or the theme. Put it in
shared/or the app layer. - Server state. The data belongs to the backend. Use a server-state library instead of copying it into your own store.
Server state is not client state
Orders, products, and profiles are copies of data the server owns. They can go stale, and they need loading and error handling. Storing them in useState or a global store means you write that logic yourself. Let the data access layer own them, as in useOrders from the previous chapter. Components then call the hook and get cached, shared data without any prop passing.
Keep UI state separate. Whether a filter panel is open is not server data, so it stays in the component.
Example: feature state in a hook
A cart feature needs its items in several components. Here the server owns the list, and the UI owns only the selected item.
// src/features/orders/hooks/useOrderSelection.ts
import { useState } from "react";
import { useOrders } from "./useOrders";
export function useOrderSelection(userId: string) {
const { data: orders = [], isLoading } = useOrders(userId);
const [selectedId, setSelectedId] = useState<string | null>(null);
const selected = orders.find((o) => o.id === selectedId) ?? null;
return { orders, isLoading, selected, select: setSelectedId };
}The hook stores only selectedId. It derives selected on each render instead of storing a copy. If the order list refreshes, selected stays correct.
Common errors
- Duplicated state. Storing both
ordersandselectedOrdercreates two values that drift apart. Store the id and derive the rest. - Premature globals. Putting form drafts or modal flags in a global store makes unrelated screens depend on them.
- One giant context. Every consumer re-renders when any value changes. Split contexts by concern, or keep the state in a hook.
- Prop drilling past two or three levels. This is a signal to move the state into a feature hook or context, not to reach for a global.
- Crossing feature boundaries. If feature A needs feature B's state, expose it through B's
index.ts. Do not import B's internals.
Exercise
A product page shows a list from the API and a "compare" checkbox on each row. The compare panel in the page header lists the checked products. Where should the product list and the checked ids live, and why not store the checked products themselves?
Show answer
The product list is server state, so it belongs in a feature hook backed by the server-state cache. The checked ids are UI state shared by the rows and the header panel, so lift them to the page or a feature hook, such as useCompare. Store only the ids and derive the product objects from the cached list. That avoids a second copy that could go stale when the list refreshes.