Module 32: Components and Props

Lesson 4 of 5 · 15–25 minutes

Lists and keys

🏠The everyday picture

A teacher with a class register knows each student by roll number, not by where they sit. If students change seats, the teacher still knows who's who.

📘What it really is

<ul className="enquiry-list">
  {enquiries.map(enquiry => (
    <li key={enquiry.id}>
      <EnquiryCard enquiry={enquiry} />
    </li>
  ))}
</ul>

🖱️Try it

Render all sample enquiries with map and key={enquiry.id}. Show only open ones in the first panel and closed ones in the second, newest first. Remove the key and read the warning; then put it back.

✅Quick check

Why not use the array index as a key in a list where items can be deleted?

Show the answer

Indexes shift when items are removed, so React matches the wrong items, causing display bugs.

What makes a good key?

Show the answer

A value that's unique among the list's items and stays the same for each item, usually its id.

← PreviousComposition and children
ProLearners Assist