Command Palette

Search for a command to run...

Hectal
Phase 14: Data Fetching and Async UI
React · Exercise 2 of 6 PreviousNext
Intermediate

One status value, an error and a retry

fetchWeather() fails the first time with Network error and succeeds after that, resolving { city: "Pune", temp: 31 } after 400 ms.

Keep the request in one state object with a status field ("loading", "error" or "success").

Show <p id="loading">Loading weather...</p> while loading; on error a <div role="alert"> with the error message and a button id="retry"; on success <p id="weather">Pune: 31 °C</p>.

Clicking #retry must load again (showing the loading text) and then the weather.

What Check tests

  1. It starts in the loading state
  2. The first failure shows an alert with the message and a #retry button
  3. Clicking #retry shows the loading text again
  4. The retry succeeds and #weather shows Pune: 31 °C
  5. The request state is one object with a status field

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.