completefrontend

Write the codeMedium

Write a review list that reads a Server Component's promise with use(), and filters it in the browser

Each tea's page, a Server Component, starts getReviews(slug) without awaiting it, so the page is sent while the reviews load, and passes the promise to ReviewList inside <Suspense>. Write app/teas/[slug]/review-list.tsx: a Client Component whose default export, ReviewList({ reviews }), takes that promise. The starter reads it in an Effect.

Read the promise with React's use(), so the boundary's fallback, Loading reviews…, shows until the reviews arrive and nothing else on the page waits for them. Then show each review as a list item, Kenji (5/5): My Sunday pot., in the order given.

Above the list, a checkbox labelled Only 5-star reviews shows only the reviews with 5 stars while it's checked; when none has 5 stars, show No 5-star reviews yet. in place of the list. A tea with no reviews at all shows just No reviews yet., with no checkbox.

Write this: app/teas/[slug]/review-list.tsx
"use client";
 
import { useEffect, useState } from "react";
import type { Review } from "@/lib/shop";
 
export default function ReviewList({ reviews }: { reviews: Promise<Review[]> }) {
  const [list, setList] = useState<Review[]>([]);
  const [onlyFiveStars, setOnlyFiveStars] = useState(false);
 
  useEffect(() => {
    reviews.then(setList);
  }, [reviews]);
 
  const shown = onlyFiveStars ? list.filter((review) => review.stars === 5) : list;
 
  return (
    <>
      <label>
        <input type="checkbox" checked={onlyFiveStars} onChange={(event) => setOnlyFiveStars(event.target.checked)} /> Only 5-star reviews
      </label>
      <ul>
        {shown.map((review) => (
          <li key={review.id}>
            {review.author} ({review.stars}/5): {review.text}
          </li>
        ))}
      </ul>
    </>
  );
}
lib/shop.ts (read-only)
import "server-only";
 
export type Review = { id: number; author: string; stars: number; text: string };
 
const names: Record<string, string> = { oolong: "Oolong tea", green: "Green tea", mint: "Mint tea" };
 
/** Each tea's reviews, newest first. The tests change these. */
export const reviews: Record<string, Review[]> = {
  oolong: [
    { id: 3, author: "Kenji", stars: 5, text: "My Sunday pot." },
    { id: 2, author: "Ana", stars: 3, text: "A little too smoky for me." },
    { id: 1, author: "Sam", stars: 5, text: "Rich and smooth." },
  ],
  green: [],
  mint: [{ id: 4, author: "Ana", stars: 4, text: "Cool and sweet." }],
};
 
/** Every reviews lookup, by slug, in order. */
export const lookups: string[] = [];
 
/** Every slug the shop sells, for generateStaticParams. */
export const slugs = Object.keys(names);
 
/** The tea's name, or undefined when the shop doesn't sell it. */
export function getTeaName(slug: string): string | undefined {
  return names[slug];
}
 
/** The tea's reviews, newest first, from the reviews service. Takes 800 ms. */
export async function getReviews(slug: string): Promise<Review[]> {
  lookups.push(slug);
  await new Promise((resolve) => setTimeout(resolve, 800));
  return reviews[slug] ?? [];
}
app/teas/[slug]/page.tsx (read-only)
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getReviews, getTeaName, slugs } from "@/lib/shop";
import ReviewList from "./review-list";
 
export function generateStaticParams() {
  return slugs.map((slug) => ({ slug }));
}
 
export default async function TeaPage({ params }: PageProps<"/teas/[slug]">) {
  const { slug } = await params;
  const name = getTeaName(slug);
  if (!name) notFound();
  // Started now and not awaited: the page is sent while the reviews load.
  const reviews = getReviews(slug);
 
  return (
    <main>
      <h1>{name}</h1>
      <h2>Reviews</h2>
      <Suspense fallback={<p>Loading reviews…</p>}>
        <ReviewList reviews={reviews} />
      </Suspense>
    </main>
  );
}
The tests your code must pass
  • lets the page render at once, with the fallback until the reviews arrive
  • shows each review in order once they arrive
  • shows only 5-star reviews while the box is checked
  • says when there are no reviews, or none with 5 stars

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…