React · Chapter 6 of 26
Pure components
By the end of this chapter you can break one of Ira's pages into components and build it as a static version first, say what makes a component pure and why React needs it to be, find impure code with StrictMode, keep several small components in one file with named exports, and move work like formatting into pure helper functions that can be checked on their own. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Thinking in components
- 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 show page for Pune 52 is the biggest page so far: the title and tagline over a poster, the cinema, Nilambari in Kothrud, open since 1968 with 320 seats, and four showtimes over the opening weekend. Next to it, the member's booking: three Balcony seats, P7 to P9, one ticket each. Written as one component it would be a hundred lines that nobody wants to change, so the first job is to see the components in the drawing.
The second job is quieter. Datta found tickets on a test page numbered "Ticket 4 of 3". Nothing was wrong with the data: the component counted in a variable outside itself, and React had called it more than once. React calls components whenever it needs to, and it expects a component to give the same answer every time for the same props. This chapter says exactly what that means, shows how to catch code that breaks it, and closes the first class: after it, you can think in components.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
ShowPage.jsx: the show page from three components
Write four components in ShowPage.jsx. Export three by name, with export function: ShowHeader({ show }) returns a <header> with a heading h1 with the show's title, a paragraph with its tagline, and a paragraph with its language, certificate and minutes, Marathi · UA 13+ · 151 min (a middle dot with a space each side). VenueInfo({ venue }) returns a <section> with a heading h2 that says Where, a paragraph with the venue's name and area, Nilambari Cinema, Kothrud, and a paragraph Open since 1968 · 320 seats (the year it opened and its seats). ShowtimeList({ showtimes }) returns a <section> with a heading h2 that says When and a list, a <ul>, with an item for each showtime, keyed by its id, whose text is its day and time, Fri 6 Nov, 11:45 am.
The default export, ShowPage({ show, venue, showtimes }), returns an <article> with ShowHeader, then VenueInfo, then ShowtimeList, each given its part of the data. Each component gets its data only from its props: the Check shows each one on its own, with Pune 52 at Nilambari Cinema and with The Tiffin Thief at Rangdeep Studio Theatre, Erandwane (open since 2009, 180 seats, English, U, 95 min). Class names for app.css: show-header, venue-info, showtime-list, show-page; the Check does not look at classes.
- Task 2
format.js: two pure helpers
Write two functions in format.js and export them by name. formatDuration(minutes) gives a show's length: formatDuration(151) is "2 h 31 min", formatDuration(120) is "2 h" (no minutes when there are none), formatDuration(45) is "45 min" (no hours when there are none). formatPrice(rupees) gives a price the Indian way, with commas for lakhs and thousands: formatPrice(220) is "₹220", formatPrice(1200) is "₹1,200", formatPrice(120000) is "₹1,20,000" (a number's toLocaleString('en-IN') groups digits this way). Both must be pure: the same number always gives the same text, and nothing outside the function is read or changed. The Check calls each function with several numbers, twice each.
When both work, remove the // before format's import in App.jsx and swap Task 2's slot line there for the commented line under it, to see them in the Preview.
- Task 3
DiwaliCountdown.jsx: the same props, the same page
Diwali is on Sunday 8 November 2026. DiwaliCountdown gets today's date in its today prop, as text such as "2026-11-06", and returns a paragraph, with className="countdown" for app.css, that says exactly: Diwali in 2 days when Diwali is two or more days away (with the number: Diwali in 15 days for "2026-10-24"); Diwali is tomorrow for "2026-11-07"; Happy Diwali! for "2026-11-08"; Diwali was on Sun 8 Nov for any later day. Date.parse("2026-11-08") minus Date.parse(today) is the time between the two days in milliseconds, and a day is 86400000.
The days come from the today prop, never from the computer's clock: the Check sets the clock to another day and shows the countdown inside StrictMode, with today set to 2026-10-24, 2026-11-06, 2026-11-07, 2026-11-08 and 2026-11-10.
- Task 4 · interview question
Interview: Tickets.jsx, the tickets that counted wrong
Tickets gets the seats of one booking in its seats prop, such as ['P7', 'P8', 'P9'], and shows a list, a <ul>, with an item for each seat, in order: Ticket 1 of 3: seat P7, Ticket 2 of 3: seat P8, Ticket 3 of 3: seat P9. In the Preview it says Ticket 4 of 3, and the numbers grow every time it renders. Fix it, so that the same seats always give the same lines. Keep the text of each line as it is. The Check shows Tickets inside StrictMode, as main.jsx does, renders it twice with the same seats, then with ['B13', 'B15'] (Ticket 1 of 2: seat B13, Ticket 2 of 2: seat B15).
Rukhsana will ask: what is a pure component, and why does StrictMode render every component twice in development?
What you will learn
- Thinking in React: boxes on the design, a name for each, a static version first, then where each box's data comes from.
- Pure components: the same props give the same JSX, and rendering changes nothing that was there before.
- What breaks purity: changing a variable outside the component, Math.random or the clock while rendering.
- StrictMode: in development, React renders every component twice to make impure code show.
- Named exports: several small components in one file, export function and import { ... }.
- Pure helpers: formatting in plain functions in a .js module, checked on their own.
More in Thinking in components
Build "Pure components" 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.