Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Basics – React + Next.js की नींव  ·  Lesson 08

Lists, Keys और Conditional Rendering

map() से list render करना

JSX के अंदर React में for loop नहीं होता। इसके बजाय JavaScript के .map() से data के array को elements के array में बदलते हैं।

Data का array['Maths','Science'].map()s => <li>{s}</li>Elements का array<li>Maths</li> <li>…
map() data को JSX में बदलता है
JSX
const subjects = ['Maths', 'Science', 'English', 'Computer']

export default function Page() {
  return (
    <ul>
      {subjects.map((s) => (
        <li key={s}>{s}</li>
      ))}
    </ul>
  )
}
• Maths • Science • English • Computer

Keys क्यों ज़रूरी हैं

Key हर list item का unique label है। List बदलने पर React keys मिलाकर जानता है कि कौन-सा item जुड़ा, हटा या खिसका, और सिर्फ़ उसी को update करता है।

अच्छी keys ✅

  • Database id: key={c.id}
  • Slug: key={post.slug}
  • Unique text जो कभी repeat न हो

Risky keys ⚠️

  • List बदल सकती हो तो array index
  • Math.random() — हर render पर बदलता है
  • दो items की एक जैसी value
Key चुनना

Objects की list

JSX – components/CourseList.jsx
const courses = [
  { id: 1, title: 'C Programming', fee: 2499, level: 'Beginner' },
  { id: 2, title: 'Next.js', fee: 4499, level: 'Intermediate' },
  { id: 3, title: 'Python + AI', fee: 3499, level: 'Beginner' },
]

export default function CourseList() {
  return (
    <div className="grid">
      {courses.map((c) => (
        <article key={c.id} className="card">
          <h3>{c.title}</h3>
          <p>₹{c.fee.toLocaleString('en-IN')} · {c.level}</p>
        </article>
      ))}
    </div>
  )
}

Render से पहले filter और sort

Normal array methods से data तैयार करें, फिर map करें। JSX को simple रखें।

JSX
const beginner = courses
  .filter((c) => c.level === 'Beginner')   // keep only beginner courses
  .sort((a, b) => a.fee - b.fee)            // cheapest first

const total = courses.reduce((sum, c) => sum + c.fee, 0)

<p>{beginner.length} beginner courses · Total fees ₹{total}</p>
{beginner.map((c) => <p key={c.id}>{c.title}</p>)}
2 beginner courses · Total fees ₹10497 C Programming Python + AI
💡 .sort() original array बदल देता है। State या props sort करते समय पहले copy करें: [...courses].sort(...)।

Conditional rendering: 4 तरीके

JSX
function Status({ isLoggedIn, cartCount, role, user }) {
  // 1. Early return
  if (!user) return <p>Loading…</p>

  return (
    <div>
      {/* 2. Ternary: one or the other */}
      {isLoggedIn ? <button>Logout</button> : <button>Login</button>}

      {/* 3. && : show only when true */}
      {cartCount > 0 && <span className="badge">{cartCount} in cart</span>}

      {/* 4. Variable chosen before JSX */}
      {role === 'admin' ? <a href="/admin">Admin panel</a> : null}
    </div>
  )
}
Patternकब use करें
if (...) return ...पूरा component बदलता है (loading, login नहीं)
a ? <X/> : <Y/>दो में से एक दिखाना
cond && <X/>दिखाना या कुछ नहीं
nullसाफ़ तौर पर कुछ नहीं दिखाना

Empty state हमेशा संभालें

JSX
{posts.length === 0 ? (
  <p className="empty">No articles yet. Check back soon! ✍️</p>
) : (
  posts.map((p) => <PostCard key={p.slug} post={p} />)
)}

EduSite का courses page search में कुछ न मिलने पर "No course found" दिखाता है — छोटी सी बात जो site को professional बनाती है।

Example: grades वाली marks table

JSX – components/MarksTable.jsx
const students = [
  { roll: 1, name: 'Riya', marks: 91 },
  { roll: 2, name: 'Aman', marks: 58 },
  { roll: 3, name: 'Sara', marks: 32 },
]

const grade = (m) => (m >= 90 ? 'A+' : m >= 60 ? 'A' : m >= 33 ? 'B' : 'Fail')

export default function MarksTable() {
  return (
    <table>
      <thead><tr><th>Roll</th><th>Name</th><th>Marks</th><th>Grade</th></tr></thead>
      <tbody>
        {students.map((s) => (
          <tr key={s.roll} style={{ color: s.marks < 33 ? 'crimson' : 'inherit' }}>
            <td>{s.roll}</td><td>{s.name}</td><td>{s.marks}</td><td>{grade(s.marks)}</td>
          </tr>
        ))}
      </tbody>
    </table>
  )
}
Roll Name Marks Grade 1 Riya 91 A+ 2 Aman 58 B 3 Sara 32 Fail (shown in red)

आम गलतियाँ

  • key भूलना → console warning और गड़बड़ updates।
  • Key को map से लौटने वाले सबसे बाहरी element की जगह अंदर वाले पर लगाना।
  • खाली होने पर {list.length && ...} 0 दिखाता है → list.length > 0 && लिखें।
  • Arrow function में curly braces के साथ return भूलना: map(c => { <li/> }) कुछ नहीं दिखाता। Parentheses लगाएँ या return जोड़ें।

सारांश

  • Arrays render करने के लिए .map(); हर item को stable unique key दें।
  • JSX से पहले filter, sort और calculation करें।
  • UI दिखाने/छिपाने के लिए early return, ternary, && या null।
  • Empty state हमेशा design करें।
✅ Practice

Input और "Add" button वाली to-do list बनाइए। खाली होने पर "Nothing to do 🎉" दिखाइए, और हर item पर ✕ button जो उसे हटाए (filter से)।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Key में क्या use करना चाहिए?
हर item के लिए unique और stable value, जैसे database id या slug। जब items जुड़ें, हटें या क्रम बदले, तब array index से बचें।
React "Each child in a list should have a unique key prop" warning क्यों देता है?
आपने .map() से बिना key के elements लौटाए। Map में लौटने वाले सबसे बाहरी element पर key={item.id} (या कोई unique value) जोड़ें।
{count &&

…

} screen पर 0 क्यों दिखाता है?
count 0 होने पर expression 0 बनता है और React वह number दिखा देता है। {count > 0 &&

…

} लिखें।
क्या JSX के अंदर if-else लिख सकते हैं?
सीधे नहीं। JSX में ternary operator use करें, या return से पहले if लिखकर JSX को variable में रखें।
← Next.js Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicPages और File-based Routing आपकी प्रगति: 8 / 37 (22%) · Lesson 9 / 37 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें