0Pricing
React Academy · Lesson

Form Actions: Replacing API Routes for Mutations

Wire Server Actions directly to HTML form actions for zero-boilerplate form mutations with full revalidation.

Form Actions: Replacing API Routes for Mutations is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Traditional Pattern

In traditional Next.js applications, form submissions require a client component with an onSubmit handler that calls fetch() to POST data to an API route. This pattern works but requires writing and maintaining both the client-side fetch logic and the server-side route handler separately.

Server Actions Simplify Forms

With Server Actions, you replace the fetch call and the API route with a single function. The form element receives the Server Action as its action prop: <form action={serverAction}>. React intercepts the submission and calls the action with the form data automatically.

Accessing Form Fields with FormData

The Server Action automatically receives a FormData object as its argument when triggered by a form. You access individual fields using formData.get('email') which returns the value as a string. All standard FormData methods are available, including getAll() for multi-select fields.

Returning Results from Actions

A Server Action can return a serializable value that the client can use. Returning an error object like { error: 'Email already exists' } or a success flag like { success: true } allows the client to update the UI accordingly without a full page reload.

Page Components Stay Simple

Because the form action is a Server Action, the page component hosting the form does not need to be a Client Component. The server renders the form as static HTML, and only the interactive parts (like error display) require client-side JavaScript. This reduces the client bundle significantly.

Progressive Enhancement Built In

A form with action={serverAction} works without JavaScript enabled. The browser performs a standard HTML form POST, the server executes the action, and the server returns the updated page. This true progressive enhancement means the form degrades gracefully in all environments.

Revalidating Data After Mutation

After a Server Action mutates data, cached page data must be refreshed. Calling revalidatePath('/dashboard') inside the action invalidates the Next.js cache for that path, causing the next request to fetch fresh data. This is the standard way to keep server-rendered pages up to date after mutations.

Granular Cache Invalidation

revalidateTag('user-data') provides finer-grained cache invalidation than revalidatePath. You tag fetch calls with next: { tags: ['user-data'] } and then invalidate all tagged data at once from within the Server Action, regardless of which paths display that data.

Redirecting After Action

Server Actions can redirect the user to a new page after a successful mutation by calling redirect('/success') from the next/navigation module. This redirect is handled server-side, which means the browser receives a proper navigation response rather than a client-side redirect.

Non-Form Mutations with startTransition

Server Actions are not limited to form submissions. For button clicks and other non-form triggers, wrap the action call in startTransition(() => action(arg)). This marks the navigation as a transition, allowing React to keep the current UI visible while the action executes on the server.

Validation Inside Actions

Server Actions are the right place for server-side validation. Validate the FormData fields, return validation errors if they fail, and only proceed with the mutation if all fields are valid. Libraries like Zod integrate well here: parse the FormData into a typed schema and return the error map on failure.

FormData Question

What does a Server Action receive as its argument when triggered by a form submission?

Lesson Recap

Server Actions replace the fetch + API route pattern for form mutations. The form action prop accepts a Server Action directly, which receives a FormData object. Actions return serializable results for client UI updates, support revalidatePath and revalidateTag for cache invalidation, and work progressively without JavaScript.

Frequently asked questions

Is the “Form Actions: Replacing API Routes for Mutations” lesson free?

Yes — the full text of “Form Actions: Replacing API Routes for Mutations” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Form Actions: Replacing API Routes for Mutations”?

Wire Server Actions directly to HTML form actions for zero-boilerplate form mutations with full revalidation. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Form Actions: Replacing API Routes for Mutations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Server Actions in React 19 and Next.js
  2. Form Actions: Replacing API Routes for Mutations
  3. useActionState and useFormStatus
  4. Progressive Enhancement with Server Actions
← Back to React Academy