React · Chapter 7 of 26
State, a component that remembers
By the end of this chapter you can give a component state with useState, say what a re-render is and what React keeps between renders, explain why a variable changed in a click never reaches the screen, read state as a snapshot, add to it safely with the updater form, and follow the rules of hooks. 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's page for Sahyadri Sessions (Sat 7 Nov, 5:00 pm, Sahyadri Lawns in Aundh) has things that change while you look at them. Lawn passes are not numbered seats, so you pick how many with a − and a + button, from 1 to 10, and the total follows: ₹500, ₹1000, ₹1500. An Interested button turns on and off. And at the lawn gate, Datta sells the last 278 seats by hand and needs a counter that goes up with every ticket.
Everything you have written so far shows data that is already there. Chapter 5's handlers run when someone clicks, but nothing they do can change what a component returned. Try the obvious thing, a variable that the click changes, and the page stays exactly as it was. This chapter is about why, and about state, the one way a component can remember something and show it changed.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
TicketCount.jsx: the lawn pass counter
TicketCount gets one prop, price (the rupees for one ticket). Keep the number of tickets with useState, starting at 1, and show, in this order: a button that says − (the minus sign; a plain - works too), a paragraph with the count (1 ticket, then 2 tickets, 3 tickets and so on), a button that says +, and a paragraph that says Total ₹ and the count times price (Total ₹500 for one ticket at 500).
A click on − takes one ticket away and a click on + adds one. The count stays between 1 and 10: the − button is disabled at 1 and the + button is disabled at 10. The Check gives TicketCount price={800}, so after two clicks on + it must show 3 tickets and Total ₹2400. Give the outer <div> className="stepper" for app.css; the Check does not look at classes.
- Task 2
Interested.jsx: a button that toggles
Make Interested show, in this order: a button that says Interested, and a paragraph that says Get a reminder before the show. The button starts off, with aria-pressed false. A click turns it on: aria-pressed true, and the paragraph says We will remind you before the show. Another click turns it off again, and so on.
aria-pressed is an attribute that tells a screen reader, and the Check, whether a toggle button is pressed; write it with a true or false value in braces. The Check shows two Interested components side by side and clicks the first: the second must stay off, because each use of a component has its own state. Give the outer <div> className="interested" for app.css; the Check does not look at classes.
- Task 3
AddThree.jsx: +3 seats adds only one
AddThree starts with 2 seats held and shows a paragraph, 2 seats held, then a button +1 seat and a button +3 seats. +1 seat works. +3 seats calls addOne three times, but the page goes from 2 seats held to 3 seats held, not 5.
Fix addOne so that calling it three times in one click adds three seats: one click on +3 seats shows 5 seats held, then +1 seat shows 6 seats held, then +3 seats shows 9 seats held. Keep the two buttons and their text.
- Task 4 · interview question
Interview: WalkIn.jsx, the gate counter that does not change
WalkIn gets one prop, left (the seats left when the gate opens). It shows a paragraph 0 sold at the gate, a paragraph with left minus the tickets sold and the words seats left (278 seats left in the Preview), and a button Sell a ticket. Now a click changes the let variable sold, as the console shows, but the page still says 0 sold at the gate. Fix it, so that each click sells one ticket: 1 sold at the gate, 277 seats left, and so on. When no seat is left, the button is disabled.
The Check gives WalkIn left={3} and clicks Sell a ticket once (1 sold at the gate, 2 seats left), then twice more (3 sold at the gate, 0 seats left, the button disabled). Then it shows a new WalkIn, which must start again at 0 sold at the gate.
Rukhsana will ask: why does changing a let variable not change the screen, and what does useState do instead?
What you will learn
- A render: React calls your component to find out what it shows, and only when it has a reason to.
- useState: a value React keeps for your component between renders, and a setter that asks for a new render.
- A re-render: React calls the function again; every local variable is made again, and state comes back from React.
- State is a snapshot: inside one render, state never changes, even right after you set it.
- The updater form: setN((n) => n + 1), for a change that depends on the latest value.
- Each use has its own state, and the rules of hooks.
More in State
Build "State, a component that remembers" 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.