
Next.js Server Actions explained with a real form
“Skip the API route. Submit a form straight to a server function and refresh the page data in one step.”
Instructor, TechSnow
1 Oct 2026 · 1 min read · Web development
A Server Action is a function that runs on the server but can be called directly from a form or a button in your component.
The old way
You wrote an API route, called it with fetch, handled loading and errors, then refetched the list. That is a lot of code for adding one row.
With a Server Action
// app/todos/page.tsx
import { revalidatePath } from "next/cache";
async function addTodo(formData) {
"use server";
const title = formData.get("title");
await db.todo.create({ data: { title } });
revalidatePath("/todos");
}
export default function Page() {
return (
<form action={addTodo}>
<input name="title" required />
<button>Add</button>
</form>
);
}The form works even before JavaScript loads, and revalidatePath refreshes the list for you.
Always validate input inside the action. Anything that reaches the server can be sent by anyone, not just your form.
Written by
AttaullahInstructor, TechSnow
Teaches web development, Python and programming fundamentals at TechSnow, with clear lessons in English and Urdu / Hindi and a real project in every course.
3 comments
Hamza K. 2 days ago
Really clear explanation. The example made it click for me.
TechSnow teamAuthor2 days ago
Glad it helped, Hamza! Try changing the example and see what breaks.
Mehwish 4 days ago
Could you write a follow-up with a bigger project?


