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 state | State parent mein |
|---|---|
| Do copies, alag values | Ek hi source of truth |
| Ek panel khule to doosra pata nahi | Parent decide karta hai kaunsa khula hai |
| Sync karne ke liye jugaar | Automatically sync |
| Bugs aur confusion | Predictable data flow |
3. Kahan use hota hai?
| Use case | Shared state |
|---|---|
| Accordion | Kaunsa panel open hai |
| Search + results | Search query |
| Temperature converter | Celsius aur Fahrenheit ek value se |
| Cart icon + product page | Cart items ka count |
| Filters + table | Selected filters |
4. Kaise kaam karta hai?
Is pattern ko yaad rakhne ka formula: data neeche jata hai, events upar aate hain.
5. Code example
Accordion: ek waqt mein ek panel
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?
| Cheez | Matlab |
|---|---|
activeIndex | State parent Accordion mein, children mein nahi |
isActive prop | Har Panel ko sirf boolean milta hai |
onShow prop | Child 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?
| Stage | Chapters |
|---|---|
| Pehle | State with useState, Props, Handling Events |
| Baad mein | useContext, useReducer |
| Aage | State Management Libraries |
Final quick revision
| Point | Detail |
|---|---|
| Kab | Do components ko same data chahiye |
| Kahan | Sab se qareebi common parent |
| Data | Props se neeche |
| Events | Function props se upar |
| Faida | Single source of truth |