Skip to content

Next.js Server Actions Explained — Server-Side Mutations

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Next.js Server Actions Explained. We cover key concepts, practical examples, and best practices to help you master this topic.

Next.js Server Actions are server-side functions callable from client components, enabling data mutations with built-in progressive enhancement, revalidation, and type safety.

What You'll Learn

  • "use server" directive
  • Server Actions in Server vs Client components
  • Form mutations with actions
  • Revalidation after mutations
  • Error handling and loading states

Why It Matters

Server Actions simplify data mutations by eliminating the need for API route boilerplate. They work without JavaScript enabled and provide automatic cache revalidation.

// app/tasks/actions.js
"use server";
import { revalidatePath } from "next/cache";
import { z } from "zod";

const taskSchema = z.object({
  title: z.string().min(1, "Title is required"),
  priority: z.enum(["low", "medium", "high"]),
});

export async function createTask(formData) {
  const validated = taskSchema.parse({
    title: formData.get("title"),
    priority: formData.get("priority"),
  });

  await db.createTask(validated);
  revalidatePath("/tasks");
}

export async function deleteTask(id) {
  await db.deleteTask(id);
  revalidatePath("/tasks");
}
// app/tasks/page.jsx
import { createTask } from "./actions";

export default function TasksPage() {
  return (
    <form action={createTask}>
      <input name="title" required placeholder="Task title" />
      <select name="priority">
        <option value="low">Low</option>
        <option value="medium">Medium</option>
        <option value="high">High</option>
      </select>
      <button type="submit">Add Task</button>
    </form>
  );
}

Expected output: Form submission calls the server action directly, validates data, persists to database, and revalidates the /tasks page route.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro