completefrontend

Write the codeMedium

Use a third-party slide show that needs useState and a render function on a Server Component page

Leaf and Kettle's home page should show its featured teas in SlideShow, from the slide-show package (here @/lib/slide-show). The package calls useState, but its files don't start with "use client", and it draws each slide with renderSlide, a function you give it. The home page is an async Server Component that loads the teas, so it can neither render SlideShow itself nor hand it a function.

Write FeaturedTeas in app/featured-teas.tsx: the page renders it as <FeaturedTeas teas={teas} />, with the teas as plain data. Render SlideShow labelled Featured teas, where each slide is an <article> with the tea's name in an <h2>, its about text, its price with two decimals ($5.50), and a link, See Oolong tea, to its page, /teas/oolong.

Start on the tea of the month, the one with teaOfTheMonth: true, or on the first tea when none is.

Write this: app/featured-teas.tsx
import { SlideShow } from "@/lib/slide-show";
import type { Tea } from "@/lib/teas";
 
export default function FeaturedTeas({ teas }: { teas: Tea[] }) {
  return <SlideShow label="Featured teas" items={teas} renderSlide={(tea) => <h2>{tea.name}</h2>} />;
}
lib/slide-show.tsx (read-only)
// Stands in for "slide-show", a package from npm. Like many packages, its files don't start with "use client".
import { useState, type ReactNode } from "react";
 
export type SlideShowProps<Item> = {
  /** What the slide show is called, for screen readers. */
  label: string;
  items: Item[];
  /** Draws one slide. */
  renderSlide: (item: Item) => ReactNode;
  /** The index of the slide to start on. */
  startAt?: number;
};
 
/** Shows one item at a time, with Previous and Next buttons that wrap around. */
export function SlideShow<Item>({ label, items, renderSlide, startAt = 0 }: SlideShowProps<Item>) {
  const [index, setIndex] = useState(startAt);
  if (items.length === 0) return null;
  const go = (step: number) => setIndex((index + step + items.length) % items.length);
 
  return (
    <section aria-roledescription="carousel" aria-label={label}>
      {renderSlide(items[index])}
      <p>
        {index + 1} of {items.length}
      </p>
      <button type="button" onClick={() => go(-1)}>
        Previous
      </button>
      <button type="button" onClick={() => go(1)}>
        Next
      </button>
    </section>
  );
}
lib/teas.ts (read-only)
export type Tea = { slug: string; name: string; about: string; price: number; teaOfTheMonth?: boolean };
 
const teas: Tea[] = [
  { slug: "green", name: "Green tea", about: "Light and fresh.", price: 4 },
  { slug: "oolong", name: "Oolong tea", about: "Rich and smooth.", price: 5.5, teaOfTheMonth: true },
  { slug: "mint", name: "Mint tea", about: "Cool and sweet.", price: 3 },
];
 
/** The teas the home page features, in the shop's order. */
export async function getFeaturedTeas(): Promise<Tea[]> {
  return teas;
}
app/page.tsx (read-only)
import { getFeaturedTeas } from "@/lib/teas";
import FeaturedTeas from "./featured-teas";
 
export default async function HomePage() {
  const teas = await getFeaturedTeas();
 
  return (
    <main>
      <h1>Leaf and Kettle</h1>
      <FeaturedTeas teas={teas} />
    </main>
  );
}
The tests your code must pass
  • is a Client Component, so the server-rendered home page can use the slide show
  • draws each slide with the tea's name, description, price, and a link to its page
  • starts on the tea of the month
  • Next and Previous move through every tea and wrap around
  • starts on the first tea when none is the tea of the month

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…