Step 8: Leave a Review
Part of the Next.js Essentials section of Coddy's React journey — lesson 46 of 47.
Challenge
MediumLet people review a dish.
- Create
app/actions.jsbeginning with'use server';and exportasync function addReview(formData). - Read
slugandtextfrom the form data, ignore an emptytext, thenawait addReviewToDb(slug, text)and callrevalidatePath('/menu/' + slug)fromnext/cache. - In the dish page,
await getReviews(slug)and add a<section>below the.detailarticle. - It holds
<form id="review-form" action={addReview}>with a hidden inputname="slug"carrying the slug, a text inputid="review-input" name="text", and<button id="review-submit" type="submit">. - Below the form render
<ul id="review-list" className="reviews">with one<li>per review, each withid={'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
2Routing & Links
Folders Are RoutesNested RoutesLinking PagesDynamic RoutesNot Found PagesRecap: Routing5Data on the Server
Async ComponentsAwaiting Route ParamsTwo Fetches at OnceLoading StatesHandling ErrorsRecap: Data Fetching8Project: Coddy Eats
Step 1: The Shell