Skip to content

Where state should live

BasicsConcept

You’ve written useState and now you’re not sure which component it belongs in. One rule answers it: keep state in the lowest component that needs it, and lift it up when two do.

01 Which component gets the useState?

A shop page has a search box and a list of results. The search box needs to remember what you typed. So does the list, because it shows only the items that match. Two components, one value. Where does it go?

In React, data flows down: a parent can pass a value to its children as props, but a child can’t read a sibling’s or a parent’s state. Where you put a piece of state decides who can see it.

02 Start in the component that uses it

If only one component reads a value, put the state there. An accordion’s open or closed flag matters to nobody else, so it lives inside the accordion:

Accordion.jsx
function Accordion({ title, children }) {
const [open, setOpen] = useState(false);
return (
<section>
<button onClick={() => setOpen(!open)}>{title}</button>
{open && children}
</section>
);
}
  • Fewer re-renders. Setting state re-renders that component and everything below it. Low state keeps the redraw small.
  • Easy to reuse. Ten accordions on a page each keep their own open, with no wiring.
  • Easy to read. Everything the state affects is in one file.

03 When two components need it, lift it

The search box started with query in its own state, which was fine until Results needed it too. The fix is called lifting state up: move the state to the closest component that is a parent of both, and pass it down.

Lifting query to the closest common parent
Step 1 of 8
Component tree
App
Page
SearchBoxquery
Resultsneeds it
Footer
State in Page
Empty
Props Page passes down
Empty
  • reads query
  • looking here
  • owns the state
  • just changed
  • re-renders

The search box keeps query in its own useState. Results also needs query to filter the list, but it can’t reach a sibling’s state.

04 The same move in code

Before, query is trapped in SearchBox. After, Page owns it, and both children get what they need as props. SearchBox reports changes through a callback instead of setting state itself.

Before
function SearchBox() {
const [query, setQuery] = useState('');
return (
<input value={query}
onChange={e => setQuery(e.target.value)} />
);
}
function Results() {
// query is stuck inside SearchBox.
// There is no way to read it from here.
return <List items={allItems} />;
}
After
function Page() {
const [query, setQuery] = useState('');
return (
<>
<SearchBox query={query} onChange={setQuery} />
<Results query={query} />
</>
);
}
function SearchBox({ query, onChange }) {
return (
<input value={query}
onChange={e => onChange(e.target.value)} />
);
}
function Results({ query }) {
const items = allItems.filter(i => i.name.includes(query));
return <List items={items} />;
}

05 Lift to the common parent, no higher

Putting all state at the top of the app also “works”, so why not do that? Because every keystroke would then re-render the whole app, and every component in between would have to pass props it doesn’t use.

In SearchBoxToo lowResults can’t see the value, so the list can’t filter.
In PageJust rightBoth readers get it; a keystroke re-renders Page and its two children.
In AppToo highIt works, but by default each keystroke re-renders App and everything under it, Footer included.
Where to put a piece of state
Who needs itPut it
One componentIn that component
Two or more componentsIn their closest common parent, passed down as props
Many components, far apartIn the common parent, shared through context instead of props at every level
Nobody: it can be worked out from other state or propsNowhere. Compute it while rendering

06 Where it goes wrong

Keep one copy of each value. If SearchBox and Page both hold a query and try to keep each other updated, they will drift apart sooner or later. One owner, and everyone else gets it as a prop.

const [q, setQ] = useState(props.query);Copying a prop into stateuseState reads its argument only on the first render. When the parent’s query changes later, this copy keeps the old value. Use the prop directly.
const [matches, setMatches] = useState([]);Storing what you can computeThe filtered list follows from query and the items. Storing it means you must remember to update it every time; computing it in render can’t go stale.
<Layout query={query}><Main query={query}>…Lifting too highPassing a prop through layers that ignore it (prop drilling) is a sign the state sits too high, or that context fits better.

07 Check yourself

Question

A Modal has an isOpen flag. Only the modal and the button inside it use it. Where should the state live?

Question

CartBadge in the header and ProductCard in the page body both need the cart. Their tree is App → Header → CartBadge and App → Shop → ProductCard. Where does the cart state go?

Question

A child does useState(props.total) and shows it. The parent’s total changes from 3 to 5. What does the child show?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?