React · Chapter 3 of 26

Lists and keys

By the end of this chapter you can show an array as a list with map, give every item a key and say what React does with it, choose keys from the data, explain why an index is a bad key when a list can change order, filter a list before showing it, and show lists of lists. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Thinking in components
  • 4 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

Parda's pages are lists. The home page shows 24 shows in Diwali week. The show page for Ghar Wapsi Express lists its 20 showtimes, day by day. A member's page lists their bookings, and the basket lists seats.

Chapter 2's App wrote <ShowCard show={shows[0]} /> three times. For 24 shows that is 24 lines, and the data does not stay still: on Wednesday Sayali adds a late show of Open Mic Thursday, a sold-out showtime drops off the list, the bookings come back newest first. The page must follow the array, whatever is in it, without anyone counting lines in App.jsx. And when a list changes, React must know which item is which, or things the user typed end up next to the wrong show.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    ShowList.jsx: every show, in a list

    shows.json holds the eight shows of Diwali week's home page, and ShowList.jsx imports it as shows. Make ShowList return a list, a <ul>, with one item, an <li>, for every show in shows, in the data's order. Each item holds a heading h3 with the show's title, then a paragraph with its language, its kind and its minutes; for the first show that is exactly:

    Ghar Wapsi Express (the h3)

    Hindi film, 142 min

    Write the item once and make one for each show from the array. Give each item a key from the data: React warns about a list without keys, and the Check fails on any warning. The Check also gives shows.json three other shows (Monsoon Mixtape, an English film of 117 min; Puneri Patya, a Marathi comedy of 70 min; Bass and Bhajans, Marathi music of 120 min), and then the list must have exactly those three items. Give the <ul> className="show-list" for app.css; the Check does not look at classes.

  2. Task 2

    Genres.jsx: a show's genres as tags

    Genres gets a show's genres in its genres prop, an array of strings such as ['comedy', 'drama']. Make Genres return a list, a <ul>, with one item, an <li>, for every genre, in order, whose text is the genre: comedy, then drama. Give the <ul> className="genres" for app.css, so the items look like tags; the Check does not look at classes.

    Each item needs a key, and strings have no id; within one show's genres each genre appears once. The Check also gives Genres ['sci-fi', 'thriller'], which must show those two items, and an empty array, which must show no items at all.

  3. Task 3

    Showtimes.jsx: one show's times, day by day

    showtimes.json holds every showtime of the opening weekend, Fri 6 to Sun 8 Nov, for all eight shows, and Showtimes.jsx imports it as showtimes. Each showtime has an id, a show (the show's id), a day, a time and a venue. Showtimes gets a show's id in its showId prop, such as "S01". Make it show only that show's showtimes, grouped by day, with the days in the data's order. For each day, show a heading h3 with the day, then a list, a <ul>, with an item, an <li>, for each of that day's showtimes, whose text is the time, a comma and the venue. For S01 that is:

    Fri 6 Nov: 12:15 pm, Kala Talkies; 12:30 pm, Moti Mahal Talkies; 9:30 pm, Kala Talkies; 10:00 pm, Moti Mahal Talkies

    Sat 7 Nov: 6:45 pm, Kala Talkies; 7:00 pm, Moti Mahal Talkies

    Sun 8 Nov: 3:30 pm, Kala Talkies; 4:00 pm, Moti Mahal Talkies

    Object.groupBy groups an array by what a function returns for each item. Every list needs keys, the days' too. The Check also gives showtimes.json other showtimes, of several shows mixed together, and tries the showIds S17 and S14.

  4. Task 4 · interview question

    Interview: BookingNotes.jsx, the note that moved

    BookingNotes gets a member's bookings in its bookings prop and shows a list item for each one: the show, the day and time; the venue and the seats; and a box labelled Note for and the show's title (Note for Lal Batti), where members leave the box office a note. A member typed "Wheelchair space" for one show, and when the bookings came back in another order, the note stood under another show. Fix it, so that a note always stays in the box of the booking it was typed for.

    The Check types Wheelchair space into the box labelled Note for Dhol Tasha, then shows the bookings with Dhol Tasha first, then with another booking cancelled; each time, the note must still be in Dhol Tasha's box. Keep the texts, the labels and the boxes as they are.

    Rukhsana will ask: what does React do with a key, and why is the index a bad key when the list can change order?

What you will learn

  • map in JSX: turn an array of data into an array of elements, which React shows in order.
  • key: the name React uses to match an item with the same item in the last render.
  • Where keys come from: ids in the data, unique among the items of one list.
  • Index keys: why the position is a bad name when the list can change order.
  • filter, then map: show only the items you want.
  • Lists of lists: groups with Object.groupBy, and a fragment with a key.

More in Thinking in components

Build "Lists and keys" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React are free. This one comes with the all labs plan.