Chapter 10: Lifting State Up

Once you master this, you’ll stop fighting with props drilling and your components will feel much more organized.

Let’s go step-by-step, like I’m sitting next to you in Mumbai with our laptops open, chai on the table, and I’m explaining it live with clear, practical examples.

1. What is “Lifting State Up”?

Lifting state up means: When two or more components need to share the same piece of state and react to its changes, you should move (lift) that state to their closest common parent component.

Why?

  • React has one-way data flow: data flows down from parent to child via props.
  • Children cannot directly change parent state.
  • So if two siblings (or cousins) need the same data, the parent becomes the “single source of truth”.

Analogy: Imagine two kids (two child components) want to play with the same toy (state). If each kid has their own toy copy, they play separately. But if they want to share and see each other’s changes → the parent (common ancestor) holds the toy and gives it to both kids.

2. Classic Example: Temperature Converter (Sharing State Between Siblings)

We’ll build two input fields:

  • Celsius input
  • Fahrenheit input

When you type in one → the other updates automatically.

Wrong way (common beginner mistake): Each input has its own state → they don’t talk to each other.

Correct way: Lift the temperature state to the parent component.

Create src/components/TemperatureConverter.tsx

tsx

Key points here:

  • State celsius lives in the parent
  • Both child inputs get the value via props
  • Both children call callbacks (onTemperatureChange) to tell the parent to update the state
  • Parent re-renders → both children get the new value

3. Best Practices for State Management (2026 Style)

Situation Where to Put State Why / Best Practice
State used only by one component Inside that component (local state) Keep it simple & encapsulated
State needed by multiple siblings Lift to closest common parent Single source of truth
State needed by deeply nested components Lift to top (or use Context) Avoid prop drilling
Very complex / global state Use Context + useReducer, or libraries like Zustand, Jotai, Redux Toolkit Scalable & clean
Form state with many fields One object in parent or React Hook Form / Formik Easier validation & submission
Data fetched from API Lift to parent that needs it, or use React Query / SWR Caching, loading, error handling

Quick rule of thumb:

“Find the closest common parent that needs the data, and put the state there.”

4. Another Real-World Example: Shopping Cart Counter (Shared Across Header & Product)

Imagine:

  • A header with cart icon showing number of items
  • A product list where you can add items to cart

State needs to be shared between Header and ProductList.

Create src/components/ShoppingCartDemo.tsx

tsx

Summary – Chapter 10 Key Takeaways

  • Lifting state up = move shared state to the closest common parent
  • Parent holds state → passes value down via props
  • Children send updates up via callback functions (onXXXChange)
  • This creates single source of truth → no bugs from duplicated state
  • For very deep nesting → consider Context (next chapters) or modern libraries
  • Rule: “State should live as close as possible to where it’s needed, but high enough so everyone who needs it can access it.”

Mini Homework

  1. Build an Accordion component with 3 questions.
  2. Only one section can be open at a time.
  3. Lift the “which section is open” state to the parent Accordion component.
  4. Each Question component gets isOpen prop and onToggle callback.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *