MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Server Actions ("use server"): Type-Safe Mutations Without API Endpoints0%
Study Materials
Next.js 15Progress 0%
1.1Next.js 15 Overview & App Router vs Pages Router Architecture1.2Core File Conventions: page.tsx, layout.tsx, template.tsx & not-found.tsx1.3Dynamic Routes ([id]), Catch-All Routes ([...slug]) & Route Groups ((group))1.4Streaming UI with loading.tsx and Suspense Boundaries
3.1Server Actions ("use server"): Type-Safe Mutations Without API Endpoints3.2Form Handling with useActionState, useFormStatus & Zod Validation3.3On-Demand Cache Revalidation with revalidatePath and revalidateTag3.4Optimistic UI Updates with React useOptimistic Hook
HomeTutorialsNext.js 15Server Actions ("use server"): Type-Safe Mutations Without API Endpoints
Form Handling with useActionState, useFormStatus & Zod Validation

Server Actions ("use server"): Type-Safe Mutations Without API Endpoints

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

In traditional web development, submitting a form required creating an API endpoint (/api/feedback), setting up body parsers, managing HTTP methods, writing client-side fetch logic, and synchronizing state. Server Actions revolutionize data mutations by allowing client components and forms to invoke asynchronous server-side functions directly using the "use server" directive.


1. What are Server Actions?

A Server Action is an asynchronous function that executes exclusively on the server. They can be invoked:

  • Directly inside HTML <form action={myAction}> elements (works even if JavaScript is disabled!).
  • Programmatically from client components via event handlers or React hooks.
  • Provide end-to-end TypeScript type safety between the UI form and the database mutation.

2. Defining Server Actions in a Dedicated File

Placing server actions in a dedicated file marked with 'use server' allows them to be shared across Server and Client Components:

1// app/actions/course-actions.ts
2'use server';
3 
4import dbConnect from '@/lib/dbConnect';
5import Course from '@/models/Course';
6import { revalidatePath } from 'next/cache';
7import { redirect } from 'next/navigation';
8 
9export async function createCourseAction(formData: FormData) {
10 await dbConnect();
11 
12 const title = formData.get('title') as string;
13 const price = Number(formData.get('price'));
14 const category = formData.get('category') as string;
15 
16 // Validation
17 if (!title || title.length < 3) {
18 throw new Error('Course title must be at least 3 characters long');
19 }
20 
21 // Database persistence
22 await Course.create({
23 title,
24 price,
25 category,
26 slug: title.toLowerCase().replace(/\s+/g, '-')
27 });
28 
29 // Revalidate cache so the courses catalog immediately shows the new course!
30 revalidatePath('/courses');
31 
32 // Redirect user to the courses list
33 redirect('/courses');
34}

3. Consuming Server Actions in Components

1// app/admin/courses/new/page.tsx
2import { createCourseAction } from '@/app/actions/course-actions';
3 
4export default function NewCoursePage() {
5 return (
6 <div className="max-w-md mx-auto p-6 bg-white rounded-xl shadow-md">
7 <h2 className="text-xl font-bold mb-4">Create New Course</h2>
8 <form action={createCourseAction} className="space-y-4">
9 <div>
10 <label className="block text-sm font-medium">Course Title</label>
11 <input name="title" required className="w-full border p-2 rounded" />
12 </div>
13 <div>
14 <label className="block text-sm font-medium">Price (INR)</label>
15 <input name="price" type="number" required className="w-full border p-2 rounded" />
16 </div>
17 <button type="submit" className="w-full bg-blue-600 text-white p-2 rounded">
18 Publish Course
19 </button>
20 </form>
21 </div>
22 );
23}

Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
What directive is required at the top of an action function or file to designate it as a Server Action?
Question 2
What is a key advantage of invoking Server Actions inside <form action={action}> compared to traditional onSubmit with fetch?
Question 3
What argument does a Server Action receive when invoked as the action attribute of an HTML <form>?
Question 4
Can a Server Action be called from an interactive Client Component with 'use client'?
Question 5
Why should you call revalidatePath('/courses') inside a Server Action that adds a new course?

Next Lesson

Form Handling with useActionState, useFormStatus & Zod Validation

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Static (SSG) vs Dynamic (SSR) Rendering & generateStaticParamsForm Handling with useActionState, useFormStatus & Zod Validation

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Next.js 15 with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext