Close the earlier lessons and build this from the brief. The starter has
createStore, a selector-based useStore, and the machine from the last
lesson. For a quick test, a focus session lasts 5 seconds.
The brief
pomodoro.js, no React:
- A store holding
{ status: "idle", remaining: FOCUS_SECONDS }. send(event)movesstatusthrough the machine. Going back toidleresetsremaining. An event the machine ignores leaves the state unchanged: the same object.- A ticker with
setIntervalthat runs whether or not anything is on screen. While focusing, it countsremainingdown once a second. At 0, it sendsFINISH.
App.jsx:
Displayshows Focusing · 3 s left, with the status label and the seconds.Controlsshows the five event buttons, each disabled when the machine wouldn't accept it. It logs Controls rendered, and must not re-render every second while the timer counts.
Acceptance checks:
- start counts down from 5, then shows On a break.
- pause stops the count; resume carries on from the same second.
- While the timer counts, Controls rendered appears only when the status changes.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: send
Inside setState, work out the next status with transition. If it's the same,
return the old state unchanged, so the store doesn't notify anyone.
Hint 2: keeping Controls quiet
Controls must select only status. Display can select remaining and
status with two calls to useStore.
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- Name two situations where an external store is a better fit than context.
- Why must a selector return an existing object or a primitive?
- What makes a state machine safer than a handful of booleans?
Completion is saved in this browser only. It does not measure mastery.