Aral Code

Lists & Conditions Lesson 9 of 13

React Lists and Keys

Render arrays as lists with map, give each item a stable key, filter and sort before rendering, and render tables from arrays of objects.

Most apps show lists: products, students, messages. In React, you turn an array of data into an array of elements with the JavaScript map method.

map to elements

Example A list from an array
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  const subjects = ["Math", "Science", "English", "Filipino", "MAPEH"];

  function SubjectList() {
    return (
      <ol style={{ fontFamily: "sans-serif" }}>
        {subjects.map((s) => <li key={s}>{s}</li>)}
      </ol>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<SubjectList />);
</script>

Keys

Each item in a list needs a key prop: a value that's unique among its siblings and stays the same for that item. React uses keys to know which item is which when the list changes (added, removed, reordered).

  • Best keys: ids from your data, like a database id or an LRN.
  • OK for fixed lists that never change: the text itself.
  • Avoid: the array index, if items can be added, removed or reordered.

Lists of objects

Example A class record table
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<style>
  table { border-collapse: collapse; font-family: sans-serif; margin: 12px; }
  th, td { border: 1px solid #ddd; padding: 6px 12px; }
  th { background: #e3f1ec; }
  .fail { color: crimson; }
</style>
<script type="text/babel">
  const students = [
    { id: 101, name: "Ana Reyes", grade: 91 },
    { id: 102, name: "Ben Santos", grade: 74 },
    { id: 103, name: "Carla Cruz", grade: 88 },
    { id: 104, name: "Dan Lim", grade: 79 },
  ];

  function ClassRecord() {
    return (
      <table>
        <thead><tr><th>#</th><th>Name</th><th>Grade</th><th>Remark</th></tr></thead>
        <tbody>
          {students.map((s, i) => (
            <tr key={s.id}>
              <td>{i + 1}</td>
              <td>{s.name}</td>
              <td>{s.grade}</td>
              <td className={s.grade < 75 ? "fail" : ""}>{s.grade >= 75 ? "Passed" : "Failed"}</td>
            </tr>
          ))}
        </tbody>
      </table>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<ClassRecord />);
</script>

The key goes on the outermost element returned by map (here, <tr>).

Filter and sort, then map

Example Search and sort a list
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
  const { useState } = React;
  const products = [
    { id: 1, name: "Pandesal", price: 25 },
    { id: 2, name: "Kape 3-in-1", price: 8 },
    { id: 3, name: "Kapeng Barako", price: 45 },
    { id: 4, name: "Ensaymada", price: 35 },
    { id: 5, name: "Itlog", price: 9 },
  ];

  function Shop() {
    const [query, setQuery] = useState("");
    const [sort, setSort] = useState("name");

    const shown = products
      .filter((p) => p.name.toLowerCase().includes(query.toLowerCase()))
      .sort((a, b) => (sort === "name" ? a.name.localeCompare(b.name) : a.price - b.price));

    return (
      <div style={{ fontFamily: "sans-serif", padding: 12 }}>
        <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search…" />{" "}
        <select value={sort} onChange={(e) => setSort(e.target.value)}>
          <option value="name">Sort by name</option>
          <option value="price">Sort by price</option>
        </select>
        <ul>
          {shown.map((p) => <li key={p.id}>{p.name} — ₱{p.price}</li>)}
        </ul>
        {shown.length === 0 && <p>No products found.</p>}
      </div>
    );
  }
  ReactDOM.createRoot(document.getElementById("root")).render(<Shop />);
</script>

Notice that the filtered list isn't stored in state. It's computed from products, query and sort on every render. Don't put values in state if you can calculate them from other state.

A list component

For bigger items, render a component per item and pass the data as props:

{students.map((s) => <StudentCard key={s.id} student={s} />)}

The key stays on the component in the map, not inside StudentCard.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which array method turns data into JSX elements?
2. What makes a good key?
3. Where does the key go when mapping to <tr> elements?
4. Should a filtered list be stored in separate state?

By Sir Reyel · Last updated 2026-12-04