React · Chapter 13 of 26
State and the tree
By the end of this chapter you can say where React keeps a component's state (at its place in the tree, not in the function), predict when state is kept and when it is reset, reset it on purpose with a key, keep the state of something hidden by keeping it in the tree, and explain why a component defined inside another component loses its input's focus at every key. 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
Ira spent Monday trying the new site the way a visitor would, and wrote down four things that felt broken.
On the review panel, she typed half a review of Ghar Wapsi Express, clicked The Tiffin Thief to compare, and her text was still in the box, now under the other show's name. One click on Post would have put a film review on a play. In the tickets box, she picked 3 tickets and pressed Large text, the switch she asked for so that older customers can read the numbers, and the count jumped back to 1. In Datta's notice editor, the Diwali notice is written in English, Marathi and Hindi; switching languages either lost the draft or showed the English words in the Marathi box. And the search box took only one letter: type Pune and you get P.
Nothing in the code looks wrong, and no warning appears. All four come from one idea you have not met yet: state belongs to a place in the tree, and React decides what to keep by comparing what is at each place.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
ReviewPanel.jsx: a fresh review for every show
ReviewForm in ReviewPanel.jsx is written: a box labelled Your review of and the show's title, a count of characters (0 of 200 characters) and a Post review button, which shows Thanks! Your review of ... is posted. Do not change it.
Write ReviewPanel({ shows }), where shows is an array of shows, each with an id and a title. It shows a heading, h2, Reviews; one button per show, with the show's title, where the chosen show's button has aria-pressed true (the first show at the start) and clicking a button chooses that show; and a <ReviewForm show={...} /> for the chosen show.
When another show is chosen, its form must start empty: the text, the count and a posted review all reset, so a review typed for one show never stays on another. The Check gives ReviewPanel its own three shows (Kaagaz Ki Nao, Ek Hota Gaon and Sahyadri Sessions), types a review for the first, then chooses the others.
- Task 2
TicketsBox.jsx: the count that reset on Large text
In TicketsBox.jsx, pick 3 tickets with the + button (named One ticket more), press Large text, and the count goes back to Tickets: 1. Fix TicketsBox so that the count stays the same when Large text is switched on and off again. TicketCount is fine as it is; do not change it.
TicketsBox shows a heading, h2, Tickets; a button Large text with aria-pressed true while it is on (false at the start); and TicketCount, which shows Tickets: 3 with its − and + buttons. With Large text on, TicketCount must still sit inside an element with className="large-text", so app.css makes it big; the Check does not look at classes, only at the count, after switching both ways.
- Task 3
NoticeTabs.jsx: three drafts that keep their words
NoticeDraft in NoticeTabs.jsx is written: a box labelled Notice in and the language (Notice in English), and a count of words. Do not change it. Write NoticeTabs, which shows a heading, h2, Diwali notice; three buttons, English, Marathi and Hindi, from languages, where the chosen one has aria-pressed true (English at the start); and the chosen language's NoticeDraft.
Only the chosen language's box may be on the page, but each language keeps its own words: type in English, switch to Marathi and its box is empty, type there, switch back, and the English words are still there. Keep all three drafts in the tree and hide the two that are not chosen with the hidden attribute on an element around each (hidden={true} hides it): the Check finds only boxes that are not hidden.
- Task 4 · interview question
Interview: SearchPage.jsx, the box that drops every key
In SearchPage.jsx, type Pune in the search box and you get P: after each key the box loses its focus. Fix SearchPage({ shows }) so that typing works. It shows a search box, an <input type="search"> labelled Search shows; a paragraph Found: 2, the number of shows that match; and a list with one item per matching show, its title. A show matches when its title contains the text in the box, ignoring capital letters and spaces at the ends; an empty box matches every show.
The box must stay the same element, with the focus, while you type a whole word: the Check types pune into it and then reads the same box. Keep SearchBox as a component of its own, defined at the top level of the file, and give it what it needs as props.
Rukhsana will ask: why does my input lose focus at every key?
What you will learn
- State lives in the tree: React keeps it for a component at a place, not inside the function.
- Same type, same place, same state: a component whose props change keeps its state.
- A different type resets it: and everything under it.
- key: a name that tells React two components are different, to reset state on purpose.
- Components at the top level: one defined inside another is a new type on every render.
- Keeping hidden state: something that leaves the tree loses its state; something hidden keeps it.
More in State
Build "State and the tree" 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.