Skip to content

Checkpoint: A Pomodoro Store

The Course
•
20 min study

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) moves status through the machine. Going back to idle resets remaining. An event the machine ignores leaves the state unchanged: the same object.
  • A ticker with setInterval that runs whether or not anything is on screen. While focusing, it counts remaining down once a second. At 0, it sends FINISH.

App.jsx:

  • Display shows Focusing · 3 s left, with the status label and the seconds.
  • Controls shows 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.
Your workspace React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build Display and Controls with useStore selectors. See the brief. export default function App() { return ( <main> <h1>Pomodoro</h1> </main> ); } // The timer as a state machine: every state lists the events it accepts, and // where each one leads. Anything not listed is ignored. export const machine = { idle: { START: "focusing" }, focusing: { PAUSE: "paused", FINISH: "onBreak", RESET: "idle" }, paused: { RESUME: "focusing", RESET: "idle" }, onBreak: { FINISH: "idle", RESET: "idle" }, }; export function transition(status, event) { return machine[status][event] ?? status; } export function can(status, event) { return event in machine[status]; } // Build the timer store here: createStore with { status, remaining }, a send // function that applies the machine, and a ticker. See the brief. import { createStore } from "./store.js"; import { transition } from "./machine.js"; export const FOCUS_SECONDS = 5; // A tiny store: state that lives outside React. Any code can read it, change // it, or subscribe to changes, including code that isn't a component. export function createStore(initialState) { let state = initialState; const listeners = new Set(); return { getState: () => state, setState(update) { const next = typeof update === "function" ? update(state) : update; if (Object.is(next, state)) return; state = next; listeners.forEach((listener) => listener()); }, subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); }, }; } import { useSyncExternalStore } from "react"; // Re-renders only when the selected part of the state changes. export function useStore(store, selector = (state) => state) { return useSyncExternalStore(store.subscribe, () => selector(store.getState())); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
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
One solution React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { can } from "./machine.js"; import { pomodoro, send } from "./pomodoro.js"; import { useStore } from "./useStore.js"; const labels = { idle: "Ready", focusing: "Focusing", paused: "Paused", onBreak: "On a break" }; function Display() { const remaining = useStore(pomodoro, (s) => s.remaining); const status = useStore(pomodoro, (s) => s.status); return <h2>{labels[status]} · {remaining} s left</h2>; } function Controls() { const status = useStore(pomodoro, (s) => s.status); console.log("Controls rendered"); return ( <div className="toolbar"> {["START", "PAUSE", "RESUME", "FINISH", "RESET"].map((event) => ( <button key={event} onClick={() => send(event)} disabled={!can(status, event)}> {event.toLowerCase()} </button> ))} </div> ); } export default function App() { return ( <main> <h1>Pomodoro</h1> <Display /> <Controls /> </main> ); } // The timer as a state machine: every state lists the events it accepts, and // where each one leads. Anything not listed is ignored. export const machine = { idle: { START: "focusing" }, focusing: { PAUSE: "paused", FINISH: "onBreak", RESET: "idle" }, paused: { RESUME: "focusing", RESET: "idle" }, onBreak: { FINISH: "idle", RESET: "idle" }, }; export function transition(status, event) { return machine[status][event] ?? status; } export function can(status, event) { return event in machine[status]; } import { createStore } from "./store.js"; import { transition } from "./machine.js"; export const FOCUS_SECONDS = 5; export const pomodoro = createStore({ status: "idle", remaining: FOCUS_SECONDS }); export function send(event) { pomodoro.setState((state) => { const status = transition(state.status, event); if (status === state.status) return state; const remaining = status === "idle" ? FOCUS_SECONDS : state.remaining; return { status, remaining }; }); } // The ticker lives outside React: it runs whether or not any component is // on screen, and only changes the store while focusing. setInterval(() => { const { status, remaining } = pomodoro.getState(); if (status !== "focusing") return; if (remaining <= 1) send("FINISH"); else pomodoro.setState((state) => ({ ...state, remaining: state.remaining - 1 })); }, 1000); // A tiny store: state that lives outside React. Any code can read it, change // it, or subscribe to changes, including code that isn't a component. export function createStore(initialState) { let state = initialState; const listeners = new Set(); return { getState: () => state, setState(update) { const next = typeof update === "function" ? update(state) : update; if (Object.is(next, state)) return; state = next; listeners.forEach((listener) => listener()); }, subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); }, }; } import { useSyncExternalStore } from "react"; // Re-renders only when the selected part of the state changes. export function useStore(store, selector = (state) => state) { return useSyncExternalStore(store.subscribe, () => selector(store.getState())); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. Name two situations where an external store is a better fit than context.
  2. Why must a selector return an existing object or a primitive?
  3. What makes a state machine safer than a handful of booleans?

Was this article helpful?

Your response is saved on this device.