Sub topic 2
Lifting State Up and Its Limits
This chapter assumes you know function components, props, JSX, useState, and how to pass functions as props. It covers the first state-sharing technique in React and where it stops working.
The problem: siblings that need the same data
Two components cannot read each other's state. If a Filter input and a ProductList both depend on the same search text, neither can own it. The fix is to move the state to their closest common parent. The parent passes the value down as a prop and passes a setter down as a callback. This is called lifting state up.
import { useState } from 'react';
function SearchBox({ query, onQueryChange }) {
return (
<input
value={query}
onChange={(event) => onQueryChange(event.target.value)}
placeholder="Search products"
/>
);
}
function ProductList({ products, query }) {
const visible = products.filter((p) =>
p.toLowerCase().includes(query.toLowerCase())
);
return (
<ul>
{visible.map((p) => (
<li key={p}>{p}</li>
))}
</ul>
);
}
export function ProductPage() {
const [query, setQuery] = useState('');
const products = ['Keyboard', 'Monitor', 'Mouse', 'Desk'];
return (
<div>
<SearchBox query={query} onQueryChange={setQuery} />
<ProductList products={products} query={query} />
</div>
);
}ProductPage owns query. SearchBox is now a controlled input: it shows what it is given and reports changes upward. Both children stay in sync because they read the same value.
How to choose where state lives
- List every component that reads or changes the value.
- Find their closest common parent.
- Put the state there, and pass down only what each child needs.
Keep state as low as it can go. Lifting higher than necessary makes more components re-render and widens the surface you have to reason about.
Where it breaks down
Lifting works well for one or two levels. It strains in three situations.
- Prop drilling. If the consumer sits five levels below the owner, every component in between must accept and forward props it never uses. Renaming a prop means editing the whole chain.
- Rigid parents. The parent must know about every piece of state its children need. As the page grows, it becomes a large component that manages unrelated concerns.
- Wide re-renders. When state changes, the owner and its children re-render. Siblings that do not use the value still render unless you memoize them.
function Page({ user, theme, onThemeChange }) {
return <Layout user={user} theme={theme} onThemeChange={onThemeChange} />;
}
function Layout({ user, theme, onThemeChange }) {
// Layout uses none of these props itself.
return <Sidebar user={user} theme={theme} onThemeChange={onThemeChange} />;
}Layout is only a pipe here. This is the signal that lifting has reached its limit.
What comes next
The patterns in later chapters each address one of these limits. Context removes the drilling. Custom hooks reuse stateful logic without a shared parent. Compound components let a parent and its children coordinate state without the consumer wiring props by hand.
Exercise
A Cart badge in the header and a CheckoutButton deep inside a page both need the item count. Where should the state go, and what is the main cost if the page is deeply nested?
Show answer
Put the state in the closest common parent of the badge and the button, and pass the count and an update callback down as props. If the button is many levels below that parent, every component in between must forward the props even though it does not use them (prop drilling). Context or restructuring with children can remove that cost.