React · Chapter 8 of 26

Objects and arrays in state

By the end of this chapter you can keep objects and arrays in state and change them the way React needs: replace them with new ones, copy an object with spread and change one field (nested ones too), add to an array, remove from it, change one item, sort a copy, and say why changing state in place does nothing. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: State
  • 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

Chapter 7's state was one number or one true or false. Tonight's booking page for Ghar Wapsi Express (Sat 7 Nov, 6:45 pm, Kala Talkies) needs more. In Balcony row Q, 9 of the 25 seats are taken, and you pick up to 6 of the others: that is an array of seat ids that grows and shrinks. Who the tickets are for is an object: a name, a phone and the alerts you want. Snacks at your seat are an array of lines, each with its own quantity.

Datta remembers 16 October, when the page showed one thing and the payment used another. In React the same mistake has a new shape: change an array in place and nothing happens at all, until something else makes the page show it. This chapter is about why, and about the few ways to make a new object or array instead.

What you build: 4 tasks

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

  1. Task 1

    SeatPicker.jsx: pick your seats

    SeatPicker gets two props: seats, an array of the row's seat ids in order (Q1 to Q25 in the Preview), and taken, an array of the ids already sold. Keep the picked seats in state, an array that starts empty.

    Show one button for each seat in seats, in that order, that says the seat's id. A taken seat's button is disabled. A picked seat's button has aria-pressed true, every other seat's aria-pressed false. After the buttons, a paragraph says No seats picked yet when nothing is picked, or else Your seats: and the picked ids in the order they were picked, joined by a comma and a space (Your seats: Q7, Q8).

    A click on a free seat picks it, at the end of the list; a click on a picked seat unpicks it. At most 6: while 6 are picked, every seat that is not picked is disabled too, and the picked ones stay enabled. Never change the array in state: give the setter a new one. The Check gives SeatPicker another row (N13 to N24, with N14 to N17, N21, N22 and N24 taken). Give the outer <div> className="seat-picker" and the buttons' <div> className="seat-row" for app.css.

  2. Task 2

    Contact.jsx: one object, changed field by field

    The contact is one object in state, already written in the starter: a name, a phone and alerts, an object with sms (true) and whatsapp (false). Make Contact show, in this order: a <label> Name for a text <input> with the id contact-name; a <label> Phone for an <input> with type tel and the id contact-phone; a button SMS with aria-pressed from alerts.sms; a button WhatsApp with aria-pressed from alerts.whatsapp; then three paragraphs: Name: and the name, Phone: and the phone, and Alerts: SMS on, WhatsApp off (on or off for each alert).

    Typing in a box changes that field only: read the box with e.target.value in its onChange. A click on SMS or WhatsApp turns that alert on or off, and only that one. The Check types Asha Kulkarni in Name and 9876543210 in Phone, then clicks WhatsApp and then SMS: each time, every other field must stay as it was. Give the outer <div> className="contact", the buttons' <div> className="alerts" and the paragraphs' <div> className="contact-summary" for app.css.

  3. Task 3

    Snacks.jsx: snacks at your seat

    Snacks gets one prop, menu: an array of snacks, each with an id, a name and a price (Popcorn, Samosa and Chai in the Preview). Keep the order in state: an array of lines, each an object with an id and a qty, that starts empty.

    Show one button for each snack in menu, in that order, that says Add and the snack's name (Add Chai). Then, when the order is empty, a paragraph No snacks yet; otherwise a list, <ul>, with one <li> for each line, in the order the snacks were first added, holding the text with the snack's name, ×, and the qty (Chai × 2), a button − and a button +.

    Add Chai adds a line Chai × 1 at the end, or adds one to the Chai line if there is one. In a line, + adds one and − takes one away; when a line reaches 0 it is removed. Change a line with map and remove one with filter; never change a line object in place. Give each <li> a key. The Check gives Snacks another menu: Vada pav, Chai and Kulfi. Give the outer <div> className="snacks" and the buttons' <div> className="menu" for app.css.

  4. Task 4 · interview question

    Interview: SavedShows.jsx, the Save button that does nothing

    SavedShows gets one prop, shows: an array of shows, each with an id, a title, a venue and a time. It shows a list with one item for each show, holding its title and a button Save, then a paragraph: Nothing saved yet when nothing is saved, or the number saved and the word saved (2 saved). A click on Save adds the show's id to saved; then that show's button says Saved and is disabled.

    Now a click does nothing: the console shows the array grew, but the page does not change. Fix it so that the first Save shows 1 saved and the second 2 saved, with only the clicked shows' buttons saying Saved. The Check gives SavedShows three other shows.

    Rukhsana will ask: seats.push(seat); setSeats(seats) does nothing. Why?

What you will learn

  • Replace, never change: React compares the old and new state with Object.is, so a changed array is still "the same".
  • Objects: a copy with one field changed, { ...booking, seatClass: 'Balcony' }, and nested objects.
  • Arrays: add with [...list, item], remove with filter, change one item with map.
  • Sorting: toSorted, which sorts a copy, never sort on state.
  • A Set in state, copied the same way.

More in State

Build "Objects and arrays in state" 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.