Menu
Coddy logo textTech

Step 8: Leave a Review

Part of the Next.js Essentials section of Coddy's React journey — lesson 46 of 47.

challenge icon

Challenge

Medium

Let people review a dish.

  1. Create app/actions.js beginning with 'use server'; and export async function addReview(formData).
  2. Read slug and text from the form data, ignore an empty text, then await addReviewToDb(slug, text) and call revalidatePath('/menu/' + slug) from next/cache.
  3. In the dish page, await getReviews(slug) and add a <section> below the .detail article.
  4. It holds <form id="review-form" action={addReview}> with a hidden input name="slug" carrying the slug, a text input id="review-input" name="text", and <button id="review-submit" type="submit">.
  5. Below the form render <ul id="review-list" className="reviews"> with one <li> per review, each with id={'review-' + index}.

Try it yourself

import Link from 'next/link';
import './globals.css';

export const metadata = {
    title: {
        default: 'Coddy Eats',
        template: '%s | Coddy Eats',
    },
};

export default function RootLayout({ children }) {
    return (
        <html lang="en">
            <body>
                <nav className="nav">
                    <Link className="brand" href="/">Coddy Eats</Link>
                    <Link id="nav-menu" href="/">Menu</Link>
                    <Link id="nav-desserts" href="/?category=dessert">Desserts</Link>
                </nav>
                <div className="container">
                    {children}
                </div>
            </body>
        </html>
    );
}

All lessons in Next.js Essentials