# Lifting State Up

> How to share data between sibling components by moving state to their closest common ancestor.

Subject: React | Lesson 20 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/lifting-state-up
Previous: [Controlled vs Uncontrolled](https://codewithanimation.in/react/controlled-vs-uncontrolled.md) | Next: [The Context API](https://codewithanimation.in/react/context-api.md)

## The Sibling Problem

Because data in React only flows downwards (One-Way Data Flow), sibling components cannot pass data directly to each other.

If `Sidebar` and `MainContent` both need to know if the menu is open, you can't keep `isOpen` inside the `Sidebar` component. `MainContent` would never be able to read it.

To fix this, you move the `useState` call up into their parent component (e.g., `App`), and pass the state down to both siblings via props. This is called **Lifting State Up**.

## Controlled Components in Practice

Lifting state up is the exact reason the 'Controlled Component' pattern exists.

When you lift state out of a component, that component loses its local state and relies entirely on props from its parent. It goes from being uncontrolled to controlled.

## The 3-Step Process

**1. Remove state from the child:** Delete the `useState` call from the child component.

**2. Add state to the parent:** Paste the `useState` call into the closest common parent of the components that need the data.

**3. Pass props down:** Pass the state value and the state setter function down to the children as props.

## Common mistake: Prop Drilling

While lifting state up is the correct way to share data, if you lift it *too* high up the tree, you end up passing props through many layers of intermediate components that don't actually need the data.

This is called 'prop drilling', and it's the primary reason global state managers (like Context or Zustand) were invented.

## Tip: Keep it as low as possible

Don't put all your state in the root `App` component! Only lift state to the *closest* common ancestor. If only two adjacent components need the data, their immediate parent should hold the state.

## Try it: Interactive Diagram

Click 'Lift State Up' to see how moving state to the parent allows data to flow downwards to both siblings.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/lifting-state-up

## Check yourself

### Question 1

Why must state be lifted up to share it between siblings?

- A. Because React enforces a one-way, top-down data flow.
- B. Because siblings are not allowed to use `useState`.
- C. To improve rendering performance.

Correct answer: A

Data in React only flows down via props. To share data sideways, you have to go up to the parent, then down to the siblings.

- Why not B: Siblings can use `useState`, but they can't share that local state with each other.
- Why not C: Lifting state actually slightly decreases performance by forcing the parent to re-render, but it's architecturally required.

### Question 2

When you lift state out of a child component, what does that child become?

- A. An uncontrolled component.
- B. A controlled component.
- C. A Server Component.

Correct answer: B

Because it no longer manages its own state and relies entirely on props from the parent, it is now 'controlled'.

- Why not A: It was uncontrolled when it managed its own state.
- Why not C: Server components are a completely different rendering architecture.

### Question 3

What is the primary downside of lifting state up too high in the component tree?

- A. Prop drilling (passing props through many intermediate components).
- B. The state might expire and get garbage collected.
- C. The child components will lose access to their CSS modules.

Correct answer: A

If state is lifted too high, you have to pass it down manually through every layer, which clutters intermediate components.

- Why not B: State doesn't expire.
- Why not C: CSS is unaffected by state location.

## Remember this

- Siblings cannot share data directly.
- Move state to the closest common parent to share it.
- Lifting state converts children into controlled components.
- Lift state only as high as necessary to avoid prop drilling.
