How Server Actions Work
0 647
โ๏ธ How Server Actions Work in React 19
React 19 introduces a groundbreaking feature: Server Actions. These allow you to perform server-side logic directly from the client without needing custom API routes, handlers, or client-side fetch calls. Think of it as a cleaner, more secure, and type-safe way to perform mutations. ๐ง ๐ฌ
๐ฆ What Are Server Actions?
A Server Action is a function that runs exclusively on the server but can be invoked directly from client components. It's perfect for:
- ๐ Form submissions
- ๐ฅ Database inserts and updates
- โ Mutations with optimistic UI
Server Actions eliminate the need for creating REST or GraphQL endpoints just to handle simple tasks.
๐ ๏ธ How to Create a Server Action
// app/actions/todoActions.js
'use server';
export async function addTodo(formData) {
const title = formData.get('title');
// Save to database or external API
await db.todo.create({ title });
}
โ
The 'use server' directive tells React to treat this function as a Server Action.
๐ค Calling a Server Action from a Form
import { addTodo } from '@/app/actions/todoActions';
export default function TodoForm() {
return (
<form action={addTodo} className="flex gap-2">
<input name="title" placeholder="Add a task" required className="border p-2"/>
<button type="submit" className="bg-blue-600 text-white px-4 py-2">
โ Add
</button>
</form>
);
}
๐จ When this form is submitted, the addTodo function is executed on the server โ no extra setup required!
๐ Handling Action Results
You can return data from an action and use it on the client (e.g., to refresh data):
// Server action
'use server';
export async function addTodo(formData) {
const title = formData.get('title');
await db.todo.create({ title });
return { success: true };
}
๐งช Advanced Usage: Client Hooks + Transitions
'use client';
import { useTransition } from 'react';
import { addTodo } from '@/app/actions/todoActions';
export default function TodoForm() {
const [isPending, startTransition] = useTransition();
return (
<form action={(formData) => {
startTransition(() => {
addTodo(formData);
});
}}>
<input name="title" />
<button type="submit" disabled={isPending}>
{isPending ? 'Adding...' : 'Add Task'}
</button>
</form>
);
}
๐ก This pattern is ideal for optimistic UI updates, allowing smoother UX during async calls.
๐ Why Server Actions Are Secure
- ๐ Logic runs entirely on the server
- ๐ No API routes are exposed to the client
- ๐ฆ No secrets or sensitive logic leaks to the browser
๐ง When to Use Server Actions
- โ Submitting or updating forms
- โ Writing to a database (e.g., Mongo, Prisma, Supabase)
- โ Sending emails, logging, or server-side logic
- โ File uploads (coming soon with async actions)
๐ Summary
- โ Server Actions are async functions that run only on the server
- โ They can be triggered from client components via forms
- โ Cleaner alternative to REST or GraphQL for many cases
- โ Built-in support for transitions, optimistic UI, and security
๐ Final Thoughts
Server Actions are a major step forward in the React ecosystem. They simplify the mental model of frontend-backend interaction while boosting performance and security. As they continue to evolve, expect them to become the new default way to handle user input in modern React apps. ๐งโ๏ธ
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:




Comments
Waiting for your comments