Lifting state up
Overview
State lives inside a component and is private to it. So what happens when two components need the same state — a search box and the results it filters, or accordion panels where only one may be open? You lift the state up: remove it from the children, put it in their closest common parent, and pass it back down.
The problem
Here, each Panel controls whether it’s open:
function Panel({ title, children }) {
const [isOpen, setIsOpen] = useState(false);
return (
<section>
<h3>{title}</h3>
{isOpen ? <p>{children}</p> : <button onClick={() => setIsOpen(true)}>Show</button>}
</section>
);
}
function Accordion() {
return (
<>
<Panel title="What’s included?">49 lessons, exercises and diagrams.</Panel>
<Panel title="Is there a certificate?">Yes, after the final project.</Panel>
</>
);
}Both panels can be open at once. To make opening one close the other, they’d need to know about each other — but state is private.
Lifting the state
Step 1: Remove state from the child. Panel receives isOpen as a prop instead:
function Panel({ title, isOpen, onShow, children }) {
return (
<section>
<h3>{title}</h3>
{isOpen ? <p>{children}</p> : <button onClick={onShow}>Show</button>}
</section>
);
}Step 2: Add state to the common parent. Instead of a boolean per panel, the parent stores which panel is active:
function Accordion() {
const [activeIndex, setActiveIndex] = useState(0);
return (
<>
<Panel
title="What’s included?"
isOpen={activeIndex === 0}
onShow={() => setActiveIndex(0)}
>
49 lessons, exercises and diagrams.
</Panel>
<Panel
title="Is there a certificate?"
isOpen={activeIndex === 1}
onShow={() => setActiveIndex(1)}
>
Yes, after the final project.
</Panel>
</>
);
}Step 3: Pass state down, and handlers to change it. isOpen flows down; onShow lets the child request a change. Now exactly one panel is open at a time, because there’s only one piece of state.
A single source of truth
For every piece of state, decide which component owns it. That component holds it with useState; everyone else gets it through props. This is the “single source of truth” principle, and it’s what makes React apps predictable: when something looks wrong on screen, you find the owner and inspect its state.
Controlled and uncontrolled components
The two versions of Panel illustrate a useful distinction:
- Uncontrolled — the first
Panelowned itsisOpenstate. Easy to use (just drop it in), but the parent can’t influence it. - Controlled — the second
Panelis driven entirely by props. More setup for the parent, but fully coordinated.
Neither is better; it depends on whether the parent needs control. Many component libraries support both: if you pass value, the component is controlled; otherwise it manages its own state.
A search example
Lifting state is the standard way to connect inputs and output in different components:
function CoursesPage({ courses }) {
const [query, setQuery] = useState('');
const results = courses.filter((c) =>
c.title.toLowerCase().includes(query.toLowerCase())
);
return (
<>
<SearchBar query={query} onQueryChange={setQuery} />
<ResultCount count={results.length} />
<CourseGrid courses={results} />
</>
);
}
function SearchBar({ query, onQueryChange }) {
return (
<input
type="search"
value={query}
onChange={(e) => onQueryChange(e.target.value)}
placeholder="Search courses"
/>
);
}Notice that the parent passes the setter itself as onQueryChange — you don’t always need a wrapper function. And the filtered results are derived in the parent, not stored, following Structuring state.
How high should state go?
Lift state to the closest common parent of all the components that use it — no higher. State that’s higher than necessary makes more components re-render and spreads knowledge around the app.
Signs you need something different:
- You’re passing the same props through several layers of components that don’t use them (“prop drilling”). Try composition with
childrenfirst (Children and composition), then context (Context). - Many unrelated parts of the app need the same data (the current user, the cart). That’s what context and external stores are for (Sharing state chapter).
- The data comes from a server. Server data is best handled by a data-fetching library (TanStack Query), not lifted
useState.
Data flows down, events flow up
Lifting state reinforces React’s one-way data flow:
- Down: state values travel from owner to children as props.
- Up: children ask for changes by calling event-handler props.
This makes changes traceable: every update to activeIndex goes through setActiveIndex in Accordion, and nothing else can change it.
Practice the reflex: when two components need to “talk”, don’t look for a way for them to reach each other — find their common parent and lift the state there.
Synced search
Build CourseSearch containing a SearchInput and a CourseResults component (siblings). Typing in the input should filter the results. SearchInput receives query and onQueryChange; CourseResults receives courses and query and shows matching titles (case-insensitive) plus a count. Add a ClearButton (a third sibling) that clears the search.
import { useState } from 'react';
const courses = [
{ id: 1, title: 'React' },
{ id: 2, title: 'React Native' },
{ id: 3, title: 'Node.js' },
];
export function CourseSearch() {
// your code here
}import { useState } from 'react';
const courses = [
{ id: 1, title: 'React' },
{ id: 2, title: 'React Native' },
{ id: 3, title: 'Node.js' },
];
function SearchInput({ query, onQueryChange }) {
return (
<input
aria-label="Search courses"
value={query}
onChange={(e) => onQueryChange(e.target.value)}
/>
);
}
function ClearButton({ onClear }) {
return <button onClick={onClear}>Clear</button>;
}
function CourseResults({ courses, query }) {
const matches = courses.filter((c) => c.title.toLowerCase().includes(query.toLowerCase()));
return (
<>
<p>{matches.length} courses</p>
<ul>
{matches.map((c) => <li key={c.id}>{c.title}</li>)}
</ul>
</>
);
}
export function CourseSearch() {
const [query, setQuery] = useState('');
return (
<div>
<SearchInput query={query} onQueryChange={setQuery} />
<ClearButton onClear={() => setQuery('')} />
<CourseResults courses={courses} query={query} />
</div>
);
}Summary
- When two components need the same state, move it to their closest common parent.
- The parent passes the value down as a prop and passes event-handler props to change it.
- Each piece of state should have a single owner — a single source of truth.
- Components driven entirely by props are ‘controlled’; components with their own state are ‘uncontrolled’.
- Lift state only as high as needed; for distant components, context may be better.