Skip to content

Checkpoint: A Study Timer

The Course
•
20 min study

Close the earlier lessons and build this from the brief.

The brief

A study timer that counts minutes you log with buttons. It isn't a real clock yet; that comes on Day 6.

  • Show 0 of 60 minutes. The 60 is a constant named GOAL.
  • Buttons +5 and +15 add minutes.
  • Reset sets the minutes back to 0, and is disabled while the minutes are already 0.
  • Once the minutes reach the goal or more, show Goal reached! in bold.
  • A Show tips button shows a list of two study tips, and its text changes to Hide tips while they're showing.

Acceptance checks:

  • Four clicks on +15 show 60 of 60 minutes and Goal reached!.
  • Reset brings back 0 of 60 minutes and removes the message.
  • No errors or warnings in the console.
Your workspace React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build the study timer here. export default function App() { return ( <main> <h1>Study timer</h1> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: how many pieces of state?

Two: the minutes, and whether the tips are showing. The goal message isn't state, because it follows from the minutes.

Hint 2: the parts you've seen

Disabling is disabled={...} on the button. The tips use &&. The toggle button's text uses a ternary.

Show one solution
One solution React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; const GOAL = 60; export default function App() { const [minutes, setMinutes] = useState(0); const [showTips, setShowTips] = useState(false); return ( <main> <h1>Study timer</h1> <p>{minutes} of {GOAL} minutes</p> {minutes >= GOAL && <p><strong>Goal reached!</strong></p>} <button onClick={() => setMinutes(minutes + 5)}>+5</button>{" "} <button onClick={() => setMinutes(minutes + 15)}>+15</button>{" "} <button onClick={() => setMinutes(0)} disabled={minutes === 0}>Reset</button> <p> <button onClick={() => setShowTips(!showTips)}> {showTips ? "Hide tips" : "Show tips"} </button> </p> {showTips && ( <ul> <li>Take a short break every 25 minutes.</li> <li>Explain today's idea out loud.</li> </ul> )} </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. Why doesn't changing an ordinary let variable update the page?
  2. What's the difference between onClick={save} and onClick={save()}?
  3. When should you pass setCount a function instead of a value?

Was this article helpful?

Your response is saved on this device.