Next.js & App Router (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What are Server Actions in Next.js (`'use server'`)?
junior LevelCode ExampleState Management
In Simple Words: Server Actions are asynchronous server functions that can be called directly from Client or Server components without manually creating REST API endpoints.
typescript
// app/actions.ts
'use server'
import { revalidatePath } from 'next/cache';
import { db } from '@/lib/db';
export async function createComment(formData: FormData) {
const content = formData.get('content') as string;
await db.comments.create({ data: { content } });
revalidatePath('/comments'); // Automatically refreshes the UI!
}
In a client or server form: <form action={createComment}>. Next.js handles the POST request, serialization, and revalidation automatically.