Skip to content
Core ConceptsChapter 14 / 37Intermediate4 min read

Lifting State Up

React lifting state up kya hai, do sibling components ke beech state share karne ke liye usay common parent mein le jana, Roman Urdu mein example ke saath.

On this page

1. Lifting State Up kya hai?

Isay "state ko upar uthana" kehte hain. Parent state ka owner banta hai, children ko value props mein milti hai aur update karne ke liye ek function prop.

2. Kyun zaroori hai?

Siblings aapas mein seedha baat nahi kar sakte. Data sirf upar se neeche jata hai. Agar dono ki apni alag state ho to woh out of sync ho jati hain.

Har child ki apni stateState parent mein
Do copies, alag valuesEk hi source of truth
Ek panel khule to doosra pata nahiParent decide karta hai kaunsa khula hai
Sync karne ke liye jugaarAutomatically sync
Bugs aur confusionPredictable data flow

3. Kahan use hota hai?

Use caseShared state
AccordionKaunsa panel open hai
Search + resultsSearch query
Temperature converterCelsius aur Fahrenheit ek value se
Cart icon + product pageCart items ka count
Filters + tableSelected filters

4. Kaise kaam karta hai?

State upar, data neeche, events upar
State common parent mein rakho
Parent value ko props se children ko deta hai
Parent update function bhi prop mein deta hai
Child event par woh function call karta hai
Parent ki state badalti hai, sab children re-render

Is pattern ko yaad rakhne ka formula: data neeche jata hai, events upar aate hain.

5. Code example

Accordion: ek waqt mein ek panel

jsx
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)}
      >
        2 se 4 din mein delivery.
      </Panel>
      <Panel
        title="Returns"
        isActive={activeIndex === 1}
        onShow={() => setActiveIndex(1)}
      >
        7 din mein return ho sakta hai.
      </Panel>
    </>
  );
}

Code kaise kaam kar raha hai?

CheezMatlab
activeIndexState parent Accordion mein, children mein nahi
isActive propHar Panel ko sirf boolean milta hai
onShow propChild ke click ko parent tak le jata hai
setActiveIndex(1)Parent update karta hai, dono panels naye props paate hain

Ab Panel ek controlled component hai: uska behaviour parent ke props se control hota hai, apni state se nahi.

6. Common mistakes

Galati: State ko har child mein copy kar dena aur effects se sync karna

Why? Do sources of truth bante hain, ek update miss ho to UI ghalat data dikhati hai.

Behtar: State ek jagah rakho, common parent mein, aur props se baanto.

Galati: State ko zaroorat se zyada upar le jana, jaise har cheez App mein

Why? Chhota sa change poore tree ko re-render karta hai aur App bohot bhaari ho jata hai.

Behtar: State ko sirf us parent tak uthao jo un sab components ka sab se qareebi common ancestor ho.

Galati: Child ko setter seedha dena aur child ko parent ki state ka shape pata hona

Why? Child parent ki internal details se jur jata hai aur reuse mushkil ho jata hai.

Behtar: Child ko meaningful event prop do, jaise onShow ya onSelect, logic parent mein rakho.

Interview tip

"Siblings ke beech data kaise share karein?" Pehla jawab hamesha lifting state up hai. Bohot gehra tree ho to context ya state management library.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
KabDo components ko same data chahiye
KahanSab se qareebi common parent
DataProps se neeche
EventsFunction props se upar
FaidaSingle source of truth