Skip to content
Core ConceptsChapter 11 / 37Beginner4 min read

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 keyStable unique key
React items ko sirf position se pehchanta haiReact har item ko id se pehchanta hai
Item delete ho to state galat row mein chali jati haiState sahi item ke saath rehti hai
Console mein key warningKoi warning nahi
Zyada DOM kaamSirf badla hua item update

3. Kahan use hota hai?

Use caseKey kya ho
Products gridproduct.id
Chat messagesmessage.id
Todo listTodo banate waqt crypto.randomUUID()
Table rowsDatabase primary key
Static menuUnique href ya slug

4. Kaise kaam karta hai?

List render aur update
Array of data
map() har item ko JSX banata hai
Har JSX ko unique key milti hai
Agle render par React keys compare karta hai
Naya item add, hata hua remove, baqi same

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

jsx
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

jsx
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?

CheezMatlab
products.map(...)Har object se ek li banta hai
key={product.id}Stable unique id, data se aati hai
filter phir mapPehle chhanto, phir render karo
<ProductRow key=... />Key component par lagti hai, uske andar ke li par nahi
Empty checkKhali 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?

Final quick revision

PointDetail
Render listarray.map()
keySiblings mein unique aur stable
Best keyData ki id
Index keySirf static list ke liye
Random keyKabhi nahi