πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Forms and Mutations

Next.js Data🟒 Free Lesson

Advertisement

Forms and Mutations

Server Actions, form handling, validation, and progressive enhancement.

Overview

Server Actions enable form submissions without API routes.

Key Concepts

  • Server Actions β€” Functions that run on the server
  • Form Actions β€” Native form submission with actions
  • Validation β€” Server-side validation with errors
  • Optimistic Updates β€” UI updates before server response
  • Progressive Enhancement β€” Forms work without JavaScript

Code Examples

// app/actions.js
'use server';
import { revalidatePath } from 'next/cache';

export async function createPost(formData) {
  const title = formData.get('title');
  const content = formData.get('content');
  
  if (!title) {
    return { error: 'Title is required' };
  }

  await db.posts.create({ data: { title, content } });
  revalidatePath('/posts');
}

// app/posts/new/page.js
import { createPost } from '../actions';

export default function NewPost() {
  return (
    <form action={createPost}>
      <input name="title" placeholder="Title" required />
      <textarea name="content" placeholder="Content" />
      <button type="submit">Create Post</button>
    </form>
  );
}

// With validation and error handling
export async function createPostWithValidation(formData) {
  const title = formData.get('title');
  
  const errors = {};
  if (!title) errors.title = 'Title is required';
  if (title && title.length < 3) errors.title = 'Title must be 3+ characters';
  
  if (Object.keys(errors).length > 0) {
    return { errors };
  }

  await db.posts.create({ data: { title } });
  revalidatePath('/posts');
  redirect('/posts');
}

// Client component with useActionState
'use client';
import { useActionState } from 'react';

function PostForm() {
  const [state, formAction, pending] = useActionState(createPostWithValidation, null);

  return (
    <form action={formAction}>
      <input name="title" />
      {state?.errors?.title && <span>{state.errors.title}</span>}
      <button disabled={pending}>Submit</button>
    </form>
  );
}

Practice

Build a form with validation, error display, and optimistic updates.

⭐

Premium Content

Forms and Mutations

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert Next.js Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement