Where state should live
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:
- 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.
- 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.
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} />;}
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.
Results can’t see the value, so the list can’t filter.Page and its two children.App and everything under it, Footer included.| Who needs it | Put it |
|---|---|
| One component | In that component |
| Two or more components | In their closest common parent, passed down as props |
| Many components, far apart | In the common parent, shared through context instead of props at every level |
| Nobody: it can be worked out from other state or props | Nowhere. 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.