That's allowed because the page, a server component, renders both. It
renders TopicDetails on the server, then passes the result to Collapsible as
children. Collapsible only decides whether to show it.
The console proves it: [server] TopicDetails rendered for … appears five
times when the page loads, while every topic is still closed:
JSX
export default function TopicDetails({ topic }) { console.log("TopicDetails rendered for", topic.title); return ( <p className="muted"> Day {topic.day} · about {topic.minutes} minutes </p> );}
This pattern keeps client components small: a client "shell" for the
interaction, with server content inside.
What a client file can't import
The other way round doesn't work. Try it: make TopicDetailsasync, and
import it directly in Collapsible.jsx to render it there. Open a topic.
What happens
<TopicDetails> is an async Client Component. Only Server Components can be
async at the moment. Everything a client file imports becomes client code too,
so TopicDetails was turned into a client component, and those can't be
async. Pass server components in as children or other props instead. Undo
the change.
State in a layout lasts
StudyTimer is a client component in the root layout. Following a link
replaces only the page, so the layout, and the timer's state, stay. That's how
sites keep things like a music player, a sidebar or a half-written search
going while you move around:
If StudyTimer were in app/page.jsx instead of the layout, what would happen
to the count when you visit About and come back?
Check your prediction
It would start again from 0. The page is replaced when you leave, so its
components, and their state, go with it.
Exercise
Open the current day
Add a defaultOpen prop to Collapsible (false unless given), and use it so
the Day 2 topics start open.
Your working spaceNext.js · 8 filesimport Link from "next/link";
import StudyTimer from "./StudyTimer.jsx";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Topics</Link>
<Link href="/about">About</Link>
<StudyTimer />
</nav>
</header>
{children}
</body>
</html>
);
}
import { getTopics } from "../lib/topics.js";
import Collapsible from "./Collapsible.jsx";
import TopicDetails from "./TopicDetails.jsx";
export default async function HomePage() {
const topics = await getTopics();
return (
<main>
<h1>Topics</h1>
{topics.map((topic) => (
<Collapsible key={topic.id} title={topic.title}>
<TopicDetails topic={topic} />
</Collapsible>
))}
</main>
);
}
"use client";
import { useState } from "react";
// A client component: it remembers whether it's open. Its children can be
// anything, including server components.
export default function Collapsible({ title, children }) {
const [open, setOpen] = useState(false);
return (
<section className="card">
<button onClick={() => setOpen(!open)} aria-expanded={open}>
{open ? "▾" : "▸"} {title}
</button>
{open && children}
</section>
);
}
"use client";
import { useEffect, useState } from "react";
// Lives in the layout, so it keeps counting while you move between pages.
export default function StudyTimer() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const id = setInterval(() => setSeconds((s) => s + 1), 1000);
return () => clearInterval(id);
}, []);
return <span className="muted">Studied: {seconds} s</span>;
}
// A server component. It runs on the server even though a client
// component shows it.
export default function TopicDetails({ topic }) {
console.log("TopicDetails rendered for", topic.title);
return (
<p className="muted">
Day {topic.day} · about {topic.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;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>The timer in the header keeps counting on every page.</p>
</main>
);
}
// 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.
Hint 1: the idea
An initial-open prop sets the starting state; it is different from controlling the open state on every render.
Hint 2: a first step
In Collapsible, accept defaultOpen with a default of false, and use it
where the open state gets its starting value. Pass defaultOpen (meaning
true) to every Collapsible first: all of them should start open and still
close when clicked. Then work out the condition for Day 2 only.
Show one solution
In Collapsible.jsx:
JavaScript
export default function Collapsible({ title, defaultOpen = false, children }) { const [open, setOpen] = useState(defaultOpen);
useState uses its argument only on the first render, so defaultOpen decides
where each panel starts, and clicks still change it afterwards. Writing
const open = defaultOpen instead would look right at first, but clicks would
never change it: nothing sets a plain prop again.
Completion is saved in this browser only. It does not measure mastery.