Recap: Server Actions
Part of the Next.js Essentials section of Coddy's React journey — lesson 33 of 47.
Challenge
MediumChapter recap: the guestbook below saves nothing and never updates. Build the whole flow: form → action → fresh page.
- In
app/actions.js, mark the file with'use server'as its first line, and after the save callrevalidatePath('/')(imported from'next/cache'). - In
app/page.jsx, importaddGuestfrom'./actions'and wrap the input and button in a<form>withid="guest-form"andaction={addGuest}. - Give the input
name="name": that's the key the action reads withformData.get('name').
Try it yourself
import './globals.css';
export const metadata = {
title: 'My App',
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
</body>
</html>
);
}All lessons in Next.js Essentials
2Routing & Links
Folders Are RoutesNested RoutesLinking PagesDynamic RoutesNot Found PagesRecap: Routing3Layouts & Metadata
The Root LayoutShared NavigationNested LayoutsPage MetadataDynamic MetadataRecap: Layouts6Server Actions
What Is a Server ActionForms That ActReading Form DataRefreshing the PageRecap: Server Actions