Lifting State Up
Learn what lifting state up means in React, moving state to a common parent so sibling components can share the same data, with a worked example.
On this page
1. What is lifting state up?
This is called "lifting state up". The parent becomes the owner of the state. Children receive the value through props, plus a function prop to update it.
2. Why does it matter?
Siblings cannot talk to each other directly. Data only flows from top to bottom. If each one has its own state, they drift out of sync.
| Each child has its own state | State lives in the parent |
|---|---|
| Two copies, different values | One source of truth |
| One panel opens and the other does not know | The parent decides which one is open |
| Hacks to keep them in sync | Automatically in sync |
| Bugs and confusion | Predictable data flow |
3. Where is it used?
| Use case | Shared state |
|---|---|
| Accordion | Which panel is open |
| Search + results | The search query |
| Temperature converter | Celsius and Fahrenheit from one value |
| Cart icon + product page | The count of cart items |
| Filters + table | The selected filters |
4. How does it work?
A simple way to remember this pattern: data flows down, events flow up.
5. Code example
Accordion: one panel at a time
import { useState } from "react";
function Panel({ title, isActive, onShow, children }) {
return (
<section>
<h3>{title}</h3>
{isActive ? <p>{children}</p> : <button onClick={onShow}>Show</button>}
</section>
);
}
export default function Accordion() {
const [activeIndex, setActiveIndex] = useState(0);
return (
<>
<Panel
title="Delivery"
isActive={activeIndex === 0}
onShow={() => setActiveIndex(0)}
>
Delivery in 2 to 4 days.
</Panel>
<Panel
title="Returns"
isActive={activeIndex === 1}
onShow={() => setActiveIndex(1)}
>
You can return it within 7 days.
</Panel>
</>
);
}How does the code work?
| Part | Meaning |
|---|---|
activeIndex | The state lives in the parent Accordion, not in the children |
isActive prop | Each Panel only receives a boolean |
onShow prop | Carries the child's click up to the parent |
setActiveIndex(1) | The parent updates, and both panels get new props |
Now Panel is a controlled component: its behavior is controlled by the parent's props, not by its own state.
6. Common mistakes
Mistake: Copying state into every child and syncing it with effects
Why? You end up with two sources of truth, and if one update is missed the UI shows the wrong data.
Better: Keep the state in one place, the common parent, and share it through props.
Mistake: Lifting state higher than needed, like putting everything in App
Why? A tiny change re-renders the whole tree and App becomes very heavy.
Better: Lift the state only as far as the closest common ancestor of the components that need it.
Mistake: Giving the child the setter directly so it needs to know the shape of the parent's state
Why? The child becomes tied to the parent's internal details, and it is hard to reuse.
Better: Give the child a meaningful event prop like onShow or onSelect, and keep the logic in the parent.
Interview tip
"How do you share data between siblings?" The first answer is always lifting state up. If the tree is very deep, use context or a state management library.
7. What to learn before and after
| Stage | Chapters |
|---|---|
| Before | State with useState, Props, Handling Events |
| After | useContext, useReducer |
| Further | State Management Libraries |
Final quick revision
| Point | Detail |
|---|---|
| When | Two components need the same data |
| Where | The closest common parent |
| Data | Down through props |
| Events | Up through function props |
| Benefit | A single source of truth |