React mein list rendering ke liye map() aur forEach() mein kya difference hai?
Answer: map() ek naya array return karta hai jo JSX elements contain karta hai, isliye wo rendering ke liye perfect hai. forEach() undefined return karta hai aur sirf side-effects ke liye use hota hai, rendering ke liye nahi.
Q2Easy
Keys ka React mein kya role hai aur ye kyun important hain?
Answer: Keys React ko help karti hain identify karne mein ki kaunse items change, add ya remove hue hain. Ye React ko efficient re-rendering mein help karta hai by tracking which elements need to be updated. Keys unique aur stable honi chahiye.
Q3Medium
Index ko key ke taur par use karna kyun problematic hai?
Answer: Jab list reorder, filter ya modify hoti hai, toh index change ho jati hai. Isse React wrong elements ko update kar sakta hai aur component state mess ho sakta hai. Example: agar aap first item delete karte ho, toh sab indices shift ho jati hain, aur React confused ho jata hai ki kaunsa element actually delete hua.
Q4Medium
Conditional rendering ke different patterns kaunse hain aur kab kaunsa use karna chahiye?
Answer: • if/else: Jab complex logic ho ya multiple conditions check karni ho
• Ternary (? :): Simple two-way conditions ke liye (if-else alternative)
• Logical && : Jab sirf true case mein kuch render karna ho
• Early return: Multiple validation checks ke liye, code readability improve karta hai
Q5Medium
{isLoggedIn && <Dashboard />} mein agar isLoggedIn false hai toh kya render hoga?
Answer: Kuch bhi render nahi hoga. Logical && operator short-circuit evaluation use karta hai. Agar left side (isLoggedIn) false hai, toh right side evaluate hi nahi hoga aur React false/null/undefined ko render nahi karta. However, agar 0 ya empty string ho, toh wo render ho jayega - ye common pitfall hai!
Q6Hard
Agar aapke paas dynamic list hai jismein unique ID nahi hai, toh aap keys kaise handle karenge?
Answer: Best approach hai ek unique ID generate karna (using libraries like uuid ya crypto.randomUUID()). Agar items stable aur unique hain, toh content ko key bana sakte ho. Last resort mein index use kar sakte ho, but sirf tab jab list static ho (no reordering, filtering, or adding/removing items). Production mein database se data aata hai jo already unique IDs have karta hai.
Q7Hard
Explain the reconciliation algorithm aur keys ka isme kya role hai?
Answer: React ka reconciliation algorithm (also called "diffing") determine karta hai ki virtual DOM mein kya change hua hai. Keys isme critical role play karti hain:
• Without keys: React element type aur position se match karta hai, which is inefficient for lists
• With keys: React keys use karke elements ko uniquely identify kar pata hai across renders
• Agar key same hai toh React element ko reuse karta hai instead of destroying and recreating
• Ye performance ko dramatically improve karta hai especially large lists mein