React · Chapter 24 of 26
Errors and Suspense
By the end of this chapter you can keep one broken part of a page from taking the rest down with an error boundary, say which errors a boundary catches and which it cannot, load a big component only when it is needed with lazy and Suspense, and read a promise while rendering with React 19's use. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real apps
- 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
Parda's show page is made of many parts: the show's details, the seat map, the Pay button, the reviews. On a busy night some part will fail: a show with a missing field, a server that is slow, a link to a show that does not exist. Sayali's rule for Diwali week is simple: one broken part must never blank the whole page, because a page that shows Pay and the seats still sells tickets. Datta wants every failure reported, so his team hears about it before the calls start.
Here is what an error does today. The poster below reads a field that a venue does not have:
const venue = { name: 'Moti Mahal Talkies' };
function Poster() {
return <p>{venue.poster.colours}</p>;
}
The Preview covers the page:
Your code threw an error
main.jsx, line 6
Cannot read properties of undefined (reading 'colours')
Underneath, React has removed everything it rendered: with the same code in the lab, the venue's <h2> beside the poster was gone too, and the page's HTML was an empty string. An error thrown while rendering, with nothing to catch it, unmounts the whole tree. Nothing you know so far can stop it: a try around JSX catches nothing, because the JSX only describes the poster, and React calls Poster later.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
ErrorBoundary.jsx: the error boundary
Make ErrorBoundary a class component (class ErrorBoundary extends Component) with the props children and onError. It shows its children. When one of them throws while rendering, it shows instead an element with role="alert" that holds a heading h2 that says Something went wrong, a paragraph with the error's message, and a button that says Try again. When it catches an error, it calls onError(error) with the error, once; onError may be missing, and then nothing is called. Try again clears the error, so that the children are shown again.
Keep the error in this.state and change it with this.setState. The Check shows ErrorBoundary around a child that throws The seat map could not load., then makes the child work and clicks Try again. Give the alert className="boundary" for app.css.
- Task 2
SeatsPanel.jsx: load the seat map only when it is wanted
Make SeatsPanel show a button that says Show seat map (give it className="show-map" for app.css). After a click, it shows <SeatMap /> from SeatMap.jsx, and while SeatMap.jsx is still loading, a paragraph that says Loading the seat map... in its place.
Load SeatMap with lazy, so that SeatMap.jsx is loaded the first time it is shown and not with SeatsPanel, and show the paragraph as the fallback of a Suspense around it. The Check makes SeatMap.jsx slow on purpose: it looks for the fallback after the click, and for the seat map when the file has loaded; it fails if SeatMap.jsx was loaded before the click.
- Task 3
ShowDetails.jsx: a show from a promise, with use and Suspense
Make ShowDetails, with the prop showPromise (a promise that gives a show), show a paragraph that says Loading the show... while the promise is waiting, and, when it gives the show, an <article> (className="show-details" for app.css) that holds a heading h2 with the title, a paragraph with the tagline and a paragraph with the language, minutes and certificate: for The Tiffin Thief, Someone at the office is eating lunches. and English, 95 min, U.
Read the promise with use, inside a Suspense whose fallback is the paragraph. When showPromise changes to another show's promise (the Check gives Raag Diwali: A night of sitar and tabla for the festival., Instrumental, 150 min, U), show that show.
- Task 4 · interview question
Interview: PayButton.jsx, the error the boundary never saw
App.jsx puts PayButton inside an ErrorBoundary, to show a message when the hold has run out, and PayButton's click calls checkHold(hold) from hold.js, which throws then. A click on Pay ₹740 shows no message, and the Preview is covered with the error. Fix PayButton (props hold, total and onPay) so that it shows a button that says Pay ₹740 (total is 740), and a click: on a hold that has run out, shows checkHold's message, Your hold on E4, E5 ran out. Pick your seats again., in an element with role="alert", and does not call onPay; on a hold that has not run out, calls onPay(hold.seats) once and shows no message.
The message may come from PayButton itself or from the boundary around it: the Check wraps PayButton in a boundary of its own and looks for the alert either way. No error may escape the click handler.
Rukhsana will ask: why does an error in onClick not reach the error boundary?
What you will learn
- Class components, just enough for the one kind that must be a class.
- Error boundaries: getDerivedStateFromError and componentDidCatch, and a fallback for one part of the page.
- What a boundary cannot catch: errors in event handlers and in code that runs later.
- Resetting a boundary with Try again, or with a key.
- lazy and Suspense: a component whose code loads the first time it is shown, with a fallback meanwhile.
- use(promise): reading a promise while rendering, inside Suspense.
More in Real apps
Build "Errors and Suspense" 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.