React · Chapter 16 of 26

Fetching data

By the end of this chapter you can load data from Parda's server in an effect, show the three states every request has (loading, the data, an error), write the async code inside the effect the way React needs it, with try, catch and finally, wait for two requests together with Promise.all, and let the user try again with a Retry button. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Effects and data
  • 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

Until now every chapter's data was a JSON file imported at the top, so it was there before the first render. Parda's real data is not: the seats left change every minute during Diwali week, 174 showtimes are being added and sold out, and a member's bookings live on the server. The page has to ask the server and show the answer when it comes, which takes time and sometimes fails. Datta's list of complaints about the old site starts with a white page that said nothing while it loaded, and a page that stayed white for good when the server was busy on the opening night of Ghar Wapsi Express.

Asking a server is talking to something outside React, so it is an effect, the main one left after chapter 15. This chapter is how to do it right.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    ShowsPage.jsx: this week's shows, from the server

    Make ShowsPage load the shows with getShows() from api.js when it appears, once, and show one of three things. While the answer is on its way: a paragraph, Loading shows... When the shows arrive: a heading, h2, Shows this week, and a list with one item per show, the title, a middle dot and the language, as Pune 52 · Marathi. If getShows fails: a paragraph with role="alert" that shows the error's message, such as The server is busy. Try again.

    Ask in an effect, never while rendering, and do not ask again after the shows arrive. The function you give useEffect is not async: write an async function inside it and call it, with try and catch around the await. The Check gives ShowsPage its own api.js, whose getShows answers with three shows when the Check wants, or fails.

  2. Task 2

    ShowDetails.jsx: a show and its showtimes, together

    Make ShowDetails({ showId }) load the show with getShow(showId) and its showtimes with getShowtimes(showId), both from api.js, and load them again when showId changes. Until both have arrived it shows a paragraph, Loading the show... When both are here: a heading, h2, with the show's title; a paragraph with its language, minutes and certificate as Marathi · 151 min · UA 13+; a heading, h3, Showtimes; and a list with one item per showtime, its when, a middle dot and its venueName, as Sat 7 Nov, 9:30 pm · Kala Talkies. If either request fails, a paragraph with role="alert" shows the error's message.

    Ask for both at once and wait for both together with Promise.all: the Check answers neither until both have been asked for, answers the showtimes first, and expects Loading the show... until the show has arrived too. It uses its own show, Lal Batti (Hindi, 158 minutes, A), with two showtimes at Chitra Mandir.

  3. Task 3

    Venues.jsx: the venues, with Retry

    Make Venues load the venues with getVenues() from api.js when it appears, once, and show one of three things. While loading: a paragraph, Loading venues... When they arrive: a heading, h2, Our venues, and a list with one item per venue, its name, a comma and its area, as Kala Talkies, Sadashiv Peth. If getVenues fails: a paragraph with role="alert" with the error's message, and a button Retry.

    Clicking Retry asks once more: Loading venues... while it waits (and no error), then the list, or the new error with Retry again. The Check fails the first request, fails the retry with another message (Still busy. Try again.), and answers the third with two venues.

  4. Task 4 · interview question

    Interview: MyBookings.jsx, the effect that was async

    MyBookings({ memberId }) loads a member's bookings with getBookings(memberId) from api.js, but its effect is an async function, so React warns in the console that useEffect must not return anything besides a function, and under StrictMode the page then breaks. Fix it with no warning, and load the bookings again when memberId changes.

    It shows one of three things. While loading: a paragraph, Loading your bookings..., also again while a new memberId's bookings load. When they arrive: a heading, h2, Your bookings, and a list with one item per booking, its id, its seats joined with a comma and a space, and what was paid (amount plus fee), all joined by middle dots, as PB70382 · N3, N4, N5, N6 · ₹880. If getBookings fails: a paragraph with role="alert" with the error's message. The Check fails on any of React's warnings, changes memberId, and also fails the request.

    Rukhsana will ask: why not useEffect(async () => ...)?

What you will learn

  • Parda's server, api.js: async functions with a delay, so the Preview shows real loading.
  • Fetching in an effect: ask once when the component appears, or again when an id changes.
  • Three states: loading, the data, and an error, each with its own page.
  • async inside the effect: why the effect itself cannot be async.
  • try, catch and finally: the data, the error, and the end of loading.
  • Promise.all: two requests at once, waited for together.
  • Retry: a count of tries as a dependency.
  • Never fetch while rendering: it never stops.

More in Effects and data

Build "Fetching data" 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.