React · Chapter 10 of 26
Lifting state up
By the end of this chapter you can make two components agree by moving their shared state to their closest common parent, pass the value down with a function that changes it, write a controlled component of your own with value and onChange props, and spot two copies of the same state before they drift apart. 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
On Pune 52's page (Tue 10 Nov, 9:30 pm, Kala Talkies) the seat map and the basket sit side by side. Click Q7 in the map and the basket must list Q7 and say Pay ₹240; press Remove Q7 in the basket and Q7 must stop glowing in the map. That is the night of 16 October all over again: two places on the page that show the same seats, and the question of how they stay the same.
With chapter 8, you would give the seat map its own array of picked seats in state. But state belongs to one component, and the basket is another component: it cannot read the seat map's state, and the seat map cannot reach into the basket. Give the basket a second array and you have two copies that must be kept in step by hand, which is exactly what React was meant to save you from.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
BookingPanel.jsx: the seat map and the basket agree
SeatMap.jsx and Basket.jsx are written already and keep no state: SeatMap shows seat buttons from its props seats, taken and picked, and calls onToggle with a seat's id on a click; Basket lists its prop picked, each with a Remove button (Remove Q7) that calls onRemove with the id, and a Pay button with the total of picked times price.
BookingPanel gets one prop, row, an object with seats, taken and price (row.json in the Preview). Keep the picked seats in BookingPanel's state, an array that starts empty. Show <SeatMap /> with seats, taken, picked and onToggle, then <Basket /> with picked, price and onRemove, inside a <div className="booking-panel">. onToggle picks a free seat at the end of the array, or unpicks a picked one; onRemove unpicks it.
A click on a seat in the map must show it in the basket with the right Pay total, and Remove in the basket must unpress it in the map. Change only BookingPanel.jsx. The Check gives BookingPanel another row: P14 to P21, with P17 and P21 taken, at ₹240.
- Task 2
DayTabs.jsx: tabs that their parent controls
DayTabs is a controlled component, like an <input>: it gets three props, days (an array of day labels, like Sat 7 Nov), value (the chosen day) and onChange (a function), and keeps no state of its own. Show one button for each day, in order, that says the day, inside a <div className="day-tabs">. The button of the day equal to value has aria-pressed true, every other one aria-pressed false. A click on a day's button calls onChange once, with that day.
The Check gives DayTabs value Sat 7 Nov and clicks Sun 8 Nov: onChange must get Sun 8 Nov, and Sat 7 Nov must stay the pressed one until the Check passes value Sun 8 Nov.
- Task 3
ShowsPage.jsx: the filters and the list agree
Filters.jsx is chapter 9's filters, now controlled: it gets value (an object with a language and kinds) and onChange, and keeps nothing. ShowList.jsx shows the shows it is given, or No shows match. Both are written already.
ShowsPage gets one prop, shows (an array of shows, each with a title, a kind and a language). Keep the filters in ShowsPage's state, starting at { language: 'All', kinds: ['film', 'play', 'comedy', 'music'] }. Show <Filters /> with value and onChange, then <ShowList /> with only the shows that match: the language is All or equal to the show's language, and the show's kind is in kinds. Wrap them in a <div className="shows-page">.
Keep only the filters in state, and work out the matching shows from them during render. The Check gives ShowsPage seven other shows, chooses Marathi, unticks Play, and so on, and reads which titles are listed.
- Task 4 · interview question
Interview: CheckoutPage.jsx, two copies drift apart
CheckoutPage gets one prop, initial: the seats held, an array like ["E7", "E8", "E9", "E10"]. It shows SeatCount, a paragraph 4 seats in your basket, and SeatList, a list with each seat and a button Remove and the seat's id (Remove E8). Now Remove E8 drops E8 from the list but the count still says 4 seats in your basket. Fix it so both always show the same seats: after Remove E8, 3 seats in your basket and a list of E7, E9 and E10.
Keep SeatCount and SeatList as two components and keep their text. The Check gives CheckoutPage the seats F4, F5, F6 and F7 and removes F5 and then F7: the count must say 3 and then 2 seats in your basket.
Rukhsana will ask: two components show the same seats and drift apart. Why, and how do you fix it?
What you will learn
- Sharing state: find the closest common parent, keep the state there, and pass it down.
- Data down, events up: the value goes down as a prop, and a function prop sends changes up.
- What re-renders: a parent's state change renders it and its children again.
- A controlled component of your own: value and onChange props, like an <input>.
- One source of truth: never copy a prop into state, never keep two copies.
More in State
Build "Lifting state up" 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.