Close the earlier lessons and build this from the brief. The starter includes
the pretend server, src/api.js.
The brief
A box that shows one course topic at a time and moves to the next every two seconds.
- Load the topics with
fetchTopics. Show Loading… while waiting. - The first attempt must fail, so you can see your error state: pass
{ fail: true }the first time only. Show Couldn't load the topics. and a Try again button that loads them again. - When loaded, show 1 of 3: Components, with the title in bold. Every 2 seconds, move to the next topic, back to the first after the last.
- A Pause button stops the rotation, and its text changes to Resume.
Acceptance checks:
- The error appears first. Try again shows Loading…, then 1 of 3: Components.
- After about 2 seconds, 2 of 3: State.
- While paused, the topic doesn't change.
- Pausing and resuming several times never makes it go faster.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: two effects
One effect loads the data, and runs again when an attempt number changes. A
second effect runs the interval, only while the topics are loaded and not
paused. Each has its own dependencies and its own cleanup.
Hint 2: one status instead of two flags
A single status state of "loading", "error" or "ready" can be simpler
than separate loading and error flags. Exactly one is always true.
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- What does an effect's dependency list mean, and what goes in it?
- When does React call an effect's cleanup function?
- Two requests for different ids are running. How does the
ignorepattern make sure only the latest one updates the page?
Completion is saved in this browser only. It does not measure mastery.