React · Chapter 11 of 26
Deriving values
By the end of this chapter you can tell what belongs in state and what does not, work out totals, counts, filtered and sorted lists during render, see why a value kept in state beside its source goes stale, and keep the id of a chosen item instead of a copy of it. 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
The checkout for Pune 52 (Sat 7 Nov, 9:30 pm, Kala Talkies) adds up the seats by class, Front ₹120 and Balcony ₹240, then ₹20 a seat for the booking fee, then the total. The search box filters 24 shows as you type. Datta's support page counts the open questions. Every one of these is a number or a list that depends on something else.
With chapters 7 to 10 you could put each of them in state too: a total, a count, a matching list, and remember to set each one in every handler that changes the seats or the text. That is the 1,900 lines of the old booking page again, in React: on 16 October one of those "remember to set it" lines was missing, and the Pay button kept ₹360. React gives you a better way, and it is shorter.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Checkout.jsx: the checkout totals
Checkout gets three props: picked, an array of seat ids (["A7", "S11"]); rows, an object that gives each row letter its seat class ({ A: "Front", ..., S: "Balcony" }); and prices, an object that gives each class its price in rupees ({ Front: 120, Middle: 180, Balcony: 240 }). A seat's row is the first letter of its id.
Show, in this order: a list, <ul>, with one <li> for each picked seat, in order, saying the seat, its class and its price, like A7, Front, ₹120 (when nothing is picked, a paragraph No seats picked instead of the list); a paragraph Seats ₹ and the sum of the seats' prices; a paragraph Booking fee ₹ and 20 for each seat; and a paragraph Total ₹ and the two added together. With A7 and S11 at Kala Talkies: Seats ₹360, Booking fee ₹40, Total ₹400. Wrap them in a <div className="checkout">.
Keep nothing in state: work every value out from the props while rendering. The Check shows Checkout with Nilambari Cinema's rows and prices (Front ₹110, Middle ₹170, Balcony ₹220) and B4, K10 and N2 picked (Seats ₹500, Booking fee ₹60, Total ₹560), then passes it other picked seats, and the totals must follow.
- Task 2
ShowSearch.jsx: search as you type
ShowSearch gets one prop, shows: an array of shows, each with an id, a title and a kind (film, play, comedy or music). Show, in this order: a <label> Search shows for an <input> with type search; a <label> Kind for a <select> with the options All, Film, Play, Comedy and Music, whose values are all, film, play, comedy and music, starting at All; a paragraph with how many shows match (24 shows, 1 show, or No shows match); and a list, <ul>, with one <li> for each matching show's title, in the order of shows. Wrap them in a <div className="show-search">.
A show matches when its title contains the typed text, with capital and small letters alike and spaces at the ends ignored, and its kind is the chosen one, or All is chosen. Both fields are controlled. Keep only the text and the kind in state; work the matching shows and the count out while rendering. The Check types, chooses kinds, and also passes new shows while text is typed: the list must follow them.
- Task 3
Basket.jsx: the Pay button that keeps the old total
Basket gets two props: initial, the seats to start with (["E7", "E8", "E9"]), and price, the price of one seat. It shows a paragraph with the number of seats and the word seats (3 seats), a list with each seat and a button Remove and the seat's id (Remove E8), and a button Pay ₹ and the seats times the price (Pay ₹540).
Now the count and the total are kept in state beside the seats, and Remove E8 forgets the total: the Pay button still says ₹540. Fix it so the count and the total always follow the seats and the price: after Remove E8, 2 seats and Pay ₹360. Keep the seats in state, started from initial. The Check removes seats, and also passes a new price while Basket is on the page: the Pay button must follow it too.
- Task 4 · interview question
Interview: SelectedShow.jsx, the show that kept old seats
SelectedShow gets one prop, shows: tonight's shows, each with an id, a title, a venue, a time and left (the seats left). It shows one button for each show, saying its time and title, and a paragraph: Pick a show, or, once one is picked, the show's title, at, its venue, and its seats left (Pune 52 at Kala Talkies: 86 seats left). The picked show's button has aria-pressed true, the others false.
Seats sell all evening, so the parent passes new shows with new numbers (in the Preview, press Someone just booked). Now the paragraph keeps the old number, because the picked show is kept as a copy in state. Fix it so the paragraph always shows the picked show's numbers from the shows prop. The Check picks Monsoon Mixtape (6 seats left), then passes shows with 4 fewer seats each: the paragraph must say 2 seats left.
Rukhsana will ask: what should not be state?
What you will learn
- Derive during render: work out anything you can from props and state, in the function body, every render.
- Redundant state goes stale: a copy of something you can work out drifts from its source.
- What is state: only what the user chose or typed, that nothing else can tell you.
- Keep ids, not copies: remember which item was chosen, and find it during render.
- When the work is slow: chapter 21's useMemo, and why you rarely need it.
More in State
Build "Deriving values" 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.