Lists & Keys
React lists keys map ke saath arrays ko render karna, key prop kyun zaroori hai aur index ko key banane ka nuqsan, Roman Urdu mein examples.
On this page
1. Lists & Keys kya hain?
Data aksar array mein hota hai: products, users, comments. map() har item ko JSX mein badal deta hai. key ek chhota sa naam tag hai jo React ko batata hai ke kaunsa JSX kis item ka hai.
2. Kyun zaroori hai?
| Bina key ya galat key | Stable unique key |
|---|---|
| React items ko sirf position se pehchanta hai | React har item ko id se pehchanta hai |
| Item delete ho to state galat row mein chali jati hai | State sahi item ke saath rehti hai |
| Console mein key warning | Koi warning nahi |
| Zyada DOM kaam | Sirf badla hua item update |
3. Kahan use hota hai?
| Use case | Key kya ho |
|---|---|
| Products grid | product.id |
| Chat messages | message.id |
| Todo list | Todo banate waqt crypto.randomUUID() |
| Table rows | Database primary key |
| Static menu | Unique href ya slug |
4. Kaise kaam karta hai?
Key sirf siblings ke beech unique honi chahiye, poori app mein nahi. Key component ko prop ki tarah nahi milti, yeh React ke liye hai.
5. Code example
Basic list
const products = [
{ id: "p1", name: "Keyboard", price: 4500 },
{ id: "p2", name: "Mouse", price: 1800 },
{ id: "p3", name: "Monitor", price: 32000 },
];
export default function ProductList() {
return (
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name}: Rs. {product.price}
</li>
))}
</ul>
);
}Filter phir map
function CheapProducts({ products }) {
const cheap = products.filter((p) => p.price < 5000);
if (cheap.length === 0) {
return <p>Koi sasta product nahi mila.</p>;
}
return (
<ul>
{cheap.map((p) => (
<ProductRow key={p.id} product={p} />
))}
</ul>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
products.map(...) | Har object se ek li banta hai |
key={product.id} | Stable unique id, data se aati hai |
filter phir map | Pehle chhanto, phir render karo |
<ProductRow key=... /> | Key component par lagti hai, uske andar ke li par nahi |
| Empty check | Khali list par message dikhana |
6. Common mistakes
Galati: Array index ko key bana dena jab list reorder, delete ya insert hoti ho
Why? Item hatne par index shift ho jate hain, React galat item ko purani state aur input value de deta hai.
Behtar: Data ki apni id use karo. Index sirf tab theek hai jab list kabhi badalti hi na ho.
Galati: Render ke dauran Math.random() ya randomUUID() se key banana
Why? Har render par nayi key milti hai, React har item ko naya samajh kar DOM aur state dobara banata hai.
Behtar: Id data banate waqt ek dafa generate karo aur item ke saath save rakho.
Galati: Key ko component ke andar wale element par lagana
Why? React ko key map se return hone wale bahar wale element par chahiye, andar lagane se warning aati hai.
Behtar: Key wahan lagao jahan map ka callback JSX return karta hai.
Interview tip
"Index as key kyun bura hai?" ka jawab ek example se do: input wali list mein beech ka item delete karo, typed text galat row mein shift ho jata hai. Yeh reconciliation ka masla hai.
7. Pehle aur baad mein kya seekhein?
| Stage | Chapters |
|---|---|
| Pehle | Conditional Rendering, Props |
| Saath mein | How React Renders |
| Baad mein | Forms & Controlled Inputs, React.memo & Performance |
Final quick revision
| Point | Detail |
|---|---|
| Render list | array.map() |
key | Siblings mein unique aur stable |
| Best key | Data ki id |
| Index key | Sirf static list ke liye |
| Random key | Kabhi nahi |