React · Chapter 22 of 26
Routing
By the end of this chapter you can give every page of a React app its own address with React Router: list the routes, link between them without loading a new page, read the changing part of an address, keep a layout around every page, catch addresses that match nothing, go to a new address from code, and keep a filter in the address. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real apps
- 5 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
Datta's team sends links to Parda's shows every day: in replies to emails, in the venues' WhatsApp groups, on the box office's notice board. A link to Pune 52 must open Pune 52's page. Sayali wants the Back button to work the way people expect, and Ira's designs have five kinds of page: the list of shows, a show, a ticket, help, and a friendly page for an address that does not exist.
Everything you have built so far lives at one address. You could keep the page in state, const [page, setPage] = useState('home'), and choose with if. It would draw the right page, but nothing outside React would know: the address never changes, so there is nothing to share; Back leaves Parda altogether; and a reload starts again at the home page, because state lives only as long as the app. The address itself has to become the state that decides the page. That is what a router does.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
SiteRoutes.jsx: Parda's addresses
Make SiteRoutes return the routes that pick Parda's page for the address, all inside one parent route whose element is <Layout /> (Task 2), so that every page has the header:
/ shows <ShowList /> (Task 5)
/shows/:id shows <ShowPage /> (Task 3), for any show's id
/bookings/:id shows <Ticket /> (given)
/help shows <Help /> (given)
any other address shows <NotFound /> (given), for example /cinemas and /shows/S02/reviews
The components are imported for you. App.jsx already puts SiteRoutes inside a MemoryRouter, so do not add another router in SiteRoutes. The Check tries each address with stand-ins of its own for the pages and for Layout, so this task passes before the others do; in the Preview, every address shows Layout's slot until Task 2 is done.
- Task 2
Layout.jsx: the layout that keeps the header
Make Layout return, inside <div className="page">: a <header className="site-header"> that holds a heading h1 that says Parda, a paragraph that says Tickets for Pune's own cinemas and stages, and a <nav> with two links, Shows to the address / and Help to the address /help; then a <main> that shows the child route's element (the page for the address); then a <footer className="site-footer"> that says Parda, Pune. Write to help@parda.example.
The links must change the page inside the app, without loading a new page. The Check puts Layout around two pages of its own (The list of shows at / and The help page at /help), clicks Help, and expects the help page in the <main> with the same <header> element still on the page.
- Task 3
ShowPage.jsx: a page for every show
ShowPage is the page at /shows/:id. Read the id from the address, find the show with that id in shows.json, and return an <article> (give it className="show-page" for app.css) that shows, for /shows/S02:
a heading h2: Pune 52
a paragraph: A lost reel, a missing projectionist. (the show's tagline)
a paragraph: Marathi, 151 min, UA 13+ (language, minutes, certificate)
a paragraph: Next show: Fri 6 Nov, 11:45 am at Nilambari Cinema, Kothrud (from show.next: day, time, venue, area)
<BookButton showtimeId="T0002" count={2} /> (Task 4), with show.next.id and 2
a link that says All shows, to the address /
For an id that no show has, such as /shows/S99, return an <article> with a heading h2 that says Show not found, a paragraph that says There is no show S99 on Parda. (with the address's id) and the link All shows to /.
Every value comes from the show with the address's id: the Check also gives shows.json other shows, such as S07, The Last Show at Moti Mahal (Seventy-eight years of a single screen.; English, 109 min, U; next on Fri 6 Nov, 7:00 pm at Moti Mahal Talkies, Camp, showtime T0015), and puts a stand-in for BookButton.
- Task 4
BookButton.jsx: book, then go to the ticket
Make BookButton, with the props showtimeId and count, return a button that says Book 2 seats (for count 2). A click calls quickBook({ showtime: showtimeId, count }) from api.js, once. While it waits, the button says Booking... and is disabled. When the booking comes back ({ id: 'PB81820' }), go to the address /bookings/PB81820, so that the app shows the ticket. If quickBook fails, show its error's message in an element with role="alert", and the button says Book 2 seats again and works.
The ticket must take the place of the page you booked from in the history: the Check starts at /, opens the page with the button, books, and then presses Back on the ticket, which must go to /, not to the Book button again.
- Task 5 · interview question
Interview: ShowList.jsx, the links that leave the app
The home page lists the 6 shows in shows.json: a heading h2 that says Now showing in Pune, then a list with one item per show, each with a heading h3 that is a link to the show's page (Pune 52 to /shows/S02), a paragraph Marathi film (language and kind) and a paragraph From ₹110. A click on a title leaves the app: the Preview goes blank and everything in the app is lost. Fix the links so that a click on Pune 52 opens /shows/S02 inside the app (the Check clicks Pune 52, then Raag Diwali, and expects each show's page from its own route). Keep everything else as it is, and the key on each item.
Rukhsana will ask: why do links inside a React app use Link and not a plain <a href>? What does a click on each one do?
What you will learn
- A router and MemoryRouter: the address becomes state that React re-renders from.
- Routes and Route: the list of addresses and the element each one shows.
- Link: a link that changes the address without loading a new page.
- useParams: the changing part of an address, such as a show's id.
- Nested routes and Outlet: a layout that stays while the page inside it changes.
- The not-found route: path="*" for every other address.
- useNavigate and useSearchParams: going somewhere from code, and filters in the address.
More in Real apps
Build "Routing" 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.