completefrontend

Write the codeEasy

Write a ReadMore Client Component that hides server-rendered content until it's opened

The tea page used to start with "use client" just for its Read more button, which sent the whole page to the browser and kept it from loading the tea's story on the server. It's a Server Component again (under the question): it loads the story in TeaStory, an async Server Component, and passes it to ReadMore as children. Write ReadMore in app/teas/[slug]/read-more.tsx.

ReadMore({ children }) shows a button, Read more, and the children in an element below it. The children start hidden, but stay on the page: hide them with the hidden attribute rather than leaving them out, so the story is in the HTML the server sends. The button shows them and then reads Show less; a second click hides them again.

Give the button aria-expanded, false while the children are hidden, and aria-controls with the id of the element around them. Two ReadMores on one page need different ids, and each opens on its own.

Write this: app/teas/[slug]/read-more.tsx
import { useState, type ReactNode } from "react";
 
export default function ReadMore({ children }: { children: ReactNode }) {
  return (
    <section>
      <button type="button">Read more</button>
      <div>{children}</div>
    </section>
  );
}
lib/teas.ts (read-only)
export type Tea = { slug: string; name: string; summary: string };
 
const teas: Tea[] = [
  { slug: "oolong", name: "Oolong tea", summary: "Rich and smooth." },
  { slug: "green", name: "Green tea", summary: "Light and fresh." },
];
 
const stories: Record<string, string[]> = {
  oolong: ["Our oolong comes from a family farm high in the hills.", "Its leaves are rolled by hand and roasted twice."],
  green: ["Picked in early spring.", "Steamed within hours of picking."],
};
 
/** The tea with this slug, or undefined when the shop doesn't sell it. */
export async function getTea(slug: string): Promise<Tea | undefined> {
  return teas.find((tea) => tea.slug === slug);
}
 
/** The tea's long story, one paragraph per item. */
export async function getStory(slug: string): Promise<string[]> {
  return stories[slug] ?? [];
}
app/teas/[slug]/page.tsx (read-only)
import { notFound } from "next/navigation";
import { getStory, getTea } from "@/lib/teas";
import ReadMore from "./read-more";
 
async function TeaStory({ slug }: { slug: string }) {
  const story = await getStory(slug);
  return (
    <>
      {story.map((paragraph) => (
        <p key={paragraph}>{paragraph}</p>
      ))}
    </>
  );
}
 
export default async function TeaPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const tea = await getTea(slug);
  if (!tea) notFound();
 
  return (
    <main>
      <h1>{tea.name}</h1>
      <p>{tea.summary}</p>
      <ReadMore>
        <TeaStory slug={tea.slug} />
      </ReadMore>
    </main>
  );
}
The tests your code must pass
  • is a Client Component, so the server-rendered tea page can show it
  • keeps the story on the page, hidden, until Read more
  • Read more shows the story, and Show less hides it again
  • the button controls the element around the children, with an id of its own
  • each one opens on its own

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…