Skip to main content

Command Palette

Search for a command to run...

Lifting State Up and sending data up via callbacks

Updated
5 min read

Great—this is the next important concept after prop drilling 👍
Let’s break it down clearly.


🔹 What is “Lifting State Up”?

Lifting state up means:

Moving state from a child component to its nearest common parent so multiple components can share and stay in sync with that data.


🔹 Why do we need it?

In React:

  • State is local to a component

  • But sometimes multiple components need the same data

👉 Instead of duplicating state, we lift it up to a common parent.


🔹 Simple Example

❌ Problem (State in each child – not synced)

function ChildA() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>A: {count}</button>;
}

function ChildB() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>B: {count}</button>;
}

👉 These are independent states (not shared)


✅ Solution: Lift State Up

✅ Parent Component

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

  return (
    <>
      <ChildA count={count} setCount={setCount} />
      <ChildB count={count} setCount={setCount} />
    </>
  );
}

✅ Child Components

function ChildA({ count, setCount }) {
  return <button onClick={() => setCount(count + 1)}>A: {count}</button>;
}

function ChildB({ count, setCount }) {
  return <button onClick={() => setCount(count + 1)}>B: {count}</button>;
}

✅ Now:

  • Both children share the same state

  • Updating one updates the other instantly


🔹 What is “Prop Up”?

This is closely related.

👉 Props go down
👉 Events/functions go up


✅ Example: Child updates parent state

function Parent() {
  const [name, setName] = useState("");

  return <Child onChange={setName} />;
}
function Child({ onChange }) {
  return (
    <input
      onChange={(e) => onChange(e.target.value)}
      placeholder="Enter name"
    />
  );
}

✅ Flow:

Child → sends data → Parent
Parent → sends state → Child

This is often called: 👉 “lifting state + sending data up via callbacks”


🔹 Benefits of Lifting State Up

✅ 1. Single Source of Truth

One place controls the data

✅ Correct → Parent controls state
❌ Wrong → Multiple copies of state

✅ 2. Data Sync Between Components

All children see the same data


✅ 3. Easier Debugging

No duplicate states floating around


✅ 4. Better Control

Parent decides:

  • what changes

  • when it updates

  • how it flows


✅ 5. Reusability

Children become more reusable (they just receive props)


🔹 Why was this introduced?

React follows:

👉 Unidirectional Data Flow (Top → Down)

To maintain this:

  • State must live at the right level

  • Components should not arbitrarily share state

👉 Lifting state up solves:

  • multiple components needing same data

  • synchronization issues


🔹 Does it solve Prop Drilling?

👉 Short answer: NO (but it helps in structuring data)


✅ What it solves:

✔ Shared state between components
✔ Data consistency


❌ What it does NOT solve:

Prop drilling still happens:

Parent (state)
  ↓
Child1
  ↓
Child2
  ↓
Child3 (uses it)

Even after lifting state, you still pass props down.


🔹 Key Difference

Concept Purpose
Prop Drilling Passing data through many layers
Lifting State Up Moving state to a shared parent

🔹 When to Use What?

✅ Use Lifting State Up when:

  • Two or more components need same data

  • Data must stay in sync


❌ Don’t rely on it when:

  • component tree is very deep → leads to prop drilling

👉 Then use:

  • Context API ✅

  • Redux / Zustand ✅


🔹 Final Mental Model

          (State lives here)
                Parent
               /      \
        Child A      Child B
          ↑             ↑
       (events)     (events)

✅ Interview Answer

Lifting state up is the process of moving state from child components to a common parent so that multiple components can share and stay synchronized with the same data. It ensures a single source of truth but does not eliminate prop drilling, as props may still need to be passed down multiple levels.


✅ One-Line Summary

👉 Lift state up to share it, but use Context to avoid prop drilling


If you want next:

✅ Side-by-side: Prop Drilling vs Context vs Redux (real example)
✅ OR small project demo (counter / form sync)

Just tell me 👍