Lifting state up and composition
Sharing state between sibling components by moving it to their closest common parent, and why composition is often a better fix than deeper prop drilling.
Lifting state up and composition
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Two sibling components cannot read or set each other's state directly — state is local to the component instance that called useState, with no sibling-to-sibling channel.
- The fix is to move ("lift") the shared state up to the nearest common parent, which then passes the value down as a prop to each sibling that needs to read it.
- To let a child trigger a change, the parent also passes down a setter function (or a function that wraps it) as a prop, so the child calls a function it was given rather than owning the state itself.
- Lifting state up is the standard fix for two components needing the same data; it is not the only fix for props traveling through many layers of components that don't use them.
- Composition — passing an already-built element or component as a prop, often as `children` — lets data and markup skip straight from a high-level parent to a deeply nested component, without every component in between accepting and re-passing a prop it never uses.
- Lifting state up and composition solve different problems: lifting is about where state should live, composition is about how it reaches a deeply nested consumer without intermediate components having to care about it.
Siblings can’t see each other’s state
Each useState call is scoped to the single component instance that made it. If two sibling components each track their own state, there is no built-in channel between them:
function TemperatureInput() {
const [value, setValue] = useState("");
return <input value={value} onChange={(e) => setValue(e.target.value)} />;
}
function TemperatureDisplay() {
// has no way to see TemperatureInput's "value" — they are siblings
}
TemperatureDisplay simply has no access to TemperatureInput’s state. They’re two separate function calls, each with its own isolated slot of memory that React tracks. This is the same per-instance isolation from the previous lesson, just now surfacing as a limitation rather than a feature.
Moving state to the common parent
The standard fix is to delete the useState call from the siblings and place it one level up, in the component that renders both of them:
function TemperatureConverter() {
const [celsius, setCelsius] = useState("");
return (
<>
<TemperatureInput value={celsius} onChange={setCelsius} />
<TemperatureDisplay celsius={celsius} />
</>
);
}
function TemperatureInput({ value, onChange }) {
return <input value={value} onChange={(e) => onChange(e.target.value)} />;
}
function TemperatureDisplay({ celsius }) {
return <p>{celsius}°C is {(celsius * 9) / 5 + 32}°F</p>;
}
TemperatureConverter now owns the single source of truth. It hands TemperatureInput the current value plus a way to change it, and hands TemperatureDisplay the same value to read. Neither sibling knows the other exists; they only know the parent that configured them. This is “lifting state up” — the state didn’t disappear, it just moved to the lowest point in the tree that both consumers share.
The pattern generalizes past two siblings: if three components all need the same piece of data, it lives in their nearest common ancestor, however many levels up that is.
The cost: props traveling through layers that don’t care
Lifting state up fixes the sharing problem, but it can create a new one. If TemperatureDisplay isn’t a direct child of TemperatureConverter but is nested three layers deeper, celsius has to be threaded through every intermediate component as a prop, even though those middle components have no use for it themselves — they just pass it along. This is “prop drilling,” and it’s a real cost of lifting state too far up a deep tree.
Composition as a different kind of fix
Composition sidesteps prop drilling instead of accepting it. Rather than handing a value down through layers, you pass an already-assembled piece of UI — often via children — straight from a component that has the data to the place it’s needed:
function Page() {
const [celsius, setCelsius] = useState("");
return (
<Layout>
<TemperatureDisplay celsius={celsius} />
</Layout>
);
}
function Layout({ children }) {
// Layout never touches celsius — it just renders whatever it was handed
return <div className="page">{children}</div>;
}
Layout sits between Page and TemperatureDisplay in the rendered tree, but it never receives or re-passes celsius — Page builds the <TemperatureDisplay celsius={celsius} /> element directly and hands the finished element to Layout as children. Layout can be reused anywhere, by anything, with no knowledge of temperature data at all. This is worth sitting with: lifting state up is about where state lives; composition is about how far it has to travel through components that don’t need it. You will often use both together — lift state to a common ancestor, then use composition to deliver it past any uninterested layers in between.
What the video covers
Codevolution’s episode, titled on the platform “Lifting State Up and Sharing State Between Components in React 19,” is a direct, current walkthrough of exactly the sibling-state-sharing mechanics above. It does not cover composition as a distinct alternative — the series instead moves toward Context as its fix for prop drilling, which is covered later in this roadmap. The composition angle above, and the linked prop-drilling episode that sets up the pain point, fill that specific gap.
Where this fits
With state now shared across components, the next building block is handling real user input — forms and controlled inputs.
Check yourself
Why can't two sibling components share state just by each calling useState with the same initial value?
Each call to useState creates an independent state slot tied to that specific component instance. Two siblings each calling useState(0) get two separate counters; updating one never updates the other, because there is no connection between the two calls.
What does "lifting state up" mean in practice?
It means removing the useState call from the sibling components that need to share a value and placing it instead in their closest common parent. The parent then passes the current value down to each sibling as a prop, and passes down a function the siblings can call to change it.
A deeply nested component needs a value that currently has to be passed as a prop through five layers of components that don't use it themselves — what is one alternative to adding that prop at every layer?
Composition: have the component higher up pass the deeply nested component (or the element that needs the value) directly as a prop, often via children, so it is placed into the tree by a parent that already has the value, skipping the middle layers entirely. Context, covered later in this roadmap, is another alternative for this specific pain point.
Is lifting state up the same thing as composition?
No. Lifting state up answers where a shared piece of state should live — in the nearest common parent of the components that need it. Composition answers how data or markup reaches a deeply nested component without every intermediate layer handling a prop it doesn't use. They're often used together, but they solve different problems.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.