Data
Intermediate

Server Actions

Implement Server Actions for form submissions, mutations, and server-side logic without API routes.

25 min
3 sections
server-actions
forms
mutations
1
2
3

01. Creating Server Actions

Section 1 of 3

Server Actions are async functions that run on the server. Mark them with 'use server' to enable client-side invocation.

typescript
// app/actions.ts
'use server';

export async function createTodo(formData: FormData) {
  const title = formData.get('title') as string;

  await db.todo.create({ data: { title } });

  revalidatePath('/todos');
  return { success: true };
}

Exercise

Create a Subscribe Action

Practice

Create a Server Action that handles email subscription form submission.

Back to Course