Project 2
Project 1 worked entirely with local state — nothing ever left the browser. This project introduces a real external dependency: a weather API. That single change brings a whole new category of things to handle correctly — the data isn't there immediately, the request can fail, and the UI needs to make sense in all three states (loading, error, and loaded), not just the happy path.
Requirements
- A text input where the user types a city name, with a search button (or Enter-to-search).
- While a search is in progress, show a clear loading indicator instead of stale or blank content.
- If the city can't be found, or the request fails, show a readable error message — not a blank screen or a crash.
- On success, display at minimum: the city name, current temperature, and a short description (e.g. "Clear sky," "Light rain").
- Searching for a new city should correctly replace the previously shown result, not append to it.
- An empty search input should not trigger a request at all.
Suggested Component Breakdown
The status state is the important design decision here — rather than separate isLoading/hasError booleans that could theoretically contradict each other, a single status value ("idle" | "loading" | "error" | "success") guarantees the UI is always in exactly one well-defined state, using the lookup-object/conditional patterns from Fundamentals Chapter 5.
A Reasonable Build Order
- Build
SearchFormfirst with just a controlled input and a submit handler that logs the typed city name — no fetching yet. - Manually call the geocoding endpoint once in the browser/Postman/curl for a known city, to see the actual JSON shape you'll be working with before writing any fetch code.
- Wire up the geocoding fetch inside an async function triggered on form submit, storing the resolved coordinates in state.
- Add the second fetch for the actual weather data once coordinates are available, setting
statusto"loading"before it starts and"success"once it resolves. - Wrap both fetches in a try/catch, setting
statusto"error"(with a stored error message) if either one fails. - Build out
WeatherDisplaylast, rendering different JSX for each of the four status values.
- Show a multi-day forecast, not just current conditions.
- Add a Celsius/Fahrenheit toggle.
- Use the browser's Geolocation API to default to the user's current location on first load.
- Cache recent searches so re-searching the same city doesn't re-fetch immediately.
- Add simple weather-appropriate icons or background colors based on the description returned.