Skip to content

Checkpoint: Plan Today

The Course
•
20 min study

Close the earlier lessons and build this from the brief.

The brief

A Plan today page. The topics come from getTopics() in lib/topics.js.

  • Each topic is a checkbox labelled with its title and · 20 min (its minutes).
  • Under the list: Nothing picked yet. until something is ticked, then 2 picked · 45 minutes, counting the ticked topics and adding their minutes.
  • The topics are loaded in a server component. Only the part that needs state is a client component.

Acceptance checks: tick Server components and Dynamic routes to see 2 picked · 45 minutes, then untick one. The console shows [server] Reading topics from the database once.

Your workspace Next.js · 4 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import { getTopics } from "../lib/topics.js"; // Build the topic picker here. See the brief. export default async function HomePage() { const topics = await getTopics(); return ( <main> <h1>Plan today</h1> <p className="muted">{topics.length} topics</p> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } // Pretend database: in a real app this would query a database or call an API. const topics = [ { id: 1, title: "Pages and routes", day: 1, minutes: 20 }, { id: 2, title: "Layouts and links", day: 1, minutes: 20 }, { id: 3, title: "Server components", day: 2, minutes: 20 }, { id: 4, title: "Client components", day: 2, minutes: 20 }, { id: 5, title: "Dynamic routes", day: 3, minutes: 25 }, ]; export async function getTopics() { console.log("Reading topics from the database"); await new Promise((resolve) => setTimeout(resolve, 200)); return topics; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the split

Keep page.jsx as it is, a server component that loads the topics. Write a client TopicPicker that takes topics as a prop and holds the ticked ids in state.

Hint 2: the totals

Don't store the count or the minutes: work them out from the ticked ids during render, with filter and reduce.

Show one solution
One solution Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import { getTopics } from "../lib/topics.js"; import TopicPicker from "./TopicPicker.jsx"; export default async function HomePage() { const topics = await getTopics(); return ( <main> <h1>Plan today</h1> <TopicPicker topics={topics} /> </main> ); } "use client"; import { useState } from "react"; export default function TopicPicker({ topics }) { const [picked, setPicked] = useState([]); const minutes = topics .filter((topic) => picked.includes(topic.id)) .reduce((sum, topic) => sum + topic.minutes, 0); function toggle(id) { setPicked(picked.includes(id) ? picked.filter((p) => p !== id) : [...picked, id]); } return ( <> <ul> {topics.map((topic) => ( <li key={topic.id}> <label> <input type="checkbox" checked={picked.includes(topic.id)} onChange={() => toggle(topic.id)} />{" "} {topic.title} <span className="muted">· {topic.minutes} min</span> </label> </li> ))} </ul> <p> {picked.length === 0 ? "Nothing picked yet." : `${picked.length} picked · ${minutes} minutes`} </p> </> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } // Pretend database: in a real app this would query a database or call an API. const topics = [ { id: 1, title: "Pages and routes", day: 1, minutes: 20 }, { id: 2, title: "Layouts and links", day: 1, minutes: 20 }, { id: 3, title: "Server components", day: 2, minutes: 20 }, { id: 4, title: "Client components", day: 2, minutes: 20 }, { id: 5, title: "Dynamic routes", day: 3, minutes: 25 }, ]; export async function getTopics() { console.log("Reading topics from the database"); await new Promise((resolve) => setTimeout(resolve, 200)); return topics; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

Check yourself

  1. What does a server component send to the browser: its code, or its result?
  2. Why can't a server component pass onClick to a client component?
  3. Where should a client component read window, and why?

Was this article helpful?

Your response is saved on this device.