Home Coding & Scripting Understanding Lifting State Up in React

Understanding Lifting State Up in React

263
0

When you start building React apps, you’ll often notice that two or more components need to share the same data. Maybe one component updates something, and another component needs to display it.
If each component keeps its own version of the data, things quickly get messy.

This is where a powerful React concept comes in:

⭐ What Is “Lifting State Up”?

Lifting state up simply means moving the shared state to the closest parent component so that all child components can use the same, single source of truth.

Instead of each child component having its own copy of the state, the parent keeps the state and passes it down through props.


🧠 Why Do We Need It?

Imagine this situation:

  • Child A updates a value
  • Child B needs to display that value
  • But each one has separate state…

You’ll get problems like:

  • Data mismatch
  • Incorrect UI
  • Extra logic to sync values

Lifting the state to the parent solves all of this.


📦 A Simple Example

Let’s say we want one button to increase a number, and another component to display it.

❌ Without Lifting State Up

Each child keeps its own state — they won’t be in sync.

Child A: count = 1  
Child B: count = 3  

That’s obviously not ideal.


✅ With Lifting State Up

We move the state to the parent:

Parent.js

import { useState } from "react";
import Display from "./Display";
import CounterButton from "./CounterButton";

export default function Parent() {
  const [count, setCount] = useState(0);

  return (
    <>
      <CounterButton count={count} setCount={setCount} />
      <Display count={count} />
    </>
  );
}

CounterButton.js

export default function CounterButton({ count, setCount }) {
  return (
    <button onClick={() => setCount(count + 1)}>
      Increase ({count})
    </button>
  );
}

Display.js

export default function Display({ count }) {
  return <h2>Current Count: {count}</h2>;
}

Now both components read from the same count value stored in Parent.


🎯 When Should We Lift State Up?

Use this pattern when:

  • Two or more components need the same value
  • One component updates something another must read
  • We feel we are duplicating state unnecessarily

If the app grows even bigger, we might later use tools like Context, Redux, or Zustand, but lifting state up remains the simplest pattern for small to medium components.


🚀 Final Thoughts

Lifting state up keeps our React components:

  • Consistent
  • Organized
  • Easy to understand
  • Easy to maintain

Whenever two components need to share data, think:
👉 Should I lift the state up to a parent?

This single pattern can help us avoid many common bugs and confusion as you continue to grow as a React developer.

Previous articleSetting Up Our First Next.js App Using TypeScript
Next articleUnderstanding Object Destructuring in JavaScript
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here