27 November 2026

Next.js Server Actions for Beginners: Build a Student Feedback Form

  Asp.Net CS By Example       27 November 2026

In traditional React apps, handling a form meant creating a separate REST endpoint, writing a fetch() call, managing loading booleans, and parsing JSON errors manually. In the Next.js App Router, Server Actions let your form call an async server function directly—with React's useActionState hook handling loading and validation messages automatically. In this tutorial, you will build a student course feedback form without writing a custom API route.

How the pieces connect

<form action={formAction}> → useActionState (isPending) → 'use server' Action → Updated State & Errors

1. Create the Server Action (app/actions.ts)

A Server Action starts with the 'use server' directive. When connected to useActionState, it receives the previous state first and the browser's FormData second:

'use server';

export type FeedbackState = {
  success: boolean;
  message: string;
  errors?: {
    comment?: string;
  };
};

export async function submitFeedback(
  _prevState: FeedbackState,
  formData: FormData
): Promise<FeedbackState> {
  const course = String(formData.get('course') ?? '').trim();
  const comment = String(formData.get('comment') ?? '').trim();

  if (comment.length < 10) {
    return {
      success: false,
      message: 'Please fix the form errors below.',
      errors: { comment: 'Feedback must be at least 10 characters long.' },
    };
  }

  // Save to database or log on the server
  console.log('Saved feedback for:', course, comment);

  return {
    success: true,
    message: `Thanks! Your feedback for ${course || 'this course'} was saved.`,
  };
}

2. Build the Client Form with useActionState (app/feedback-form.tsx)

Because hooks run in the browser, mark the form component with 'use client' and connect submitFeedback via useActionState:

'use client';

import { useActionState } from 'react';
import { submitFeedback, type FeedbackState } from './actions';

const initialState: FeedbackState = {
  success: false,
  message: '',
};

export default function FeedbackForm() {
  const [state, formAction, isPending] = useActionState(
    submitFeedback,
    initialState
  );

  return (
    <form action={formAction}>
      <label htmlFor="course">Course Name</label>
      <input id="course" name="course" defaultValue="ASP.NET Core" required />

      <label htmlFor="comment">Your Feedback</label>
      <textarea id="comment" name="comment" rows={3} required />
      {state.errors?.comment && <p>{state.errors.comment}</p>}

      <button type="submit" disabled={isPending}>
        {isPending ? 'Saving...' : 'Submit Feedback'}
      </button>

      {state.message && <p>{state.message}</p>}
    </form>
  );
}

3. Render the Form in app/page.tsx

import FeedbackForm from './feedback-form';

export default function HomePage() {
  return (
    <main>
      <h1>Student Course Feedback</h1>
      <FeedbackForm />
    </main>
  );
}

Understand the Key Ideas

  1. 'use server' ensures submitFeedback runs strictly on the server—safe for database writes and secrets.
  2. useActionState returns [state, formAction, isPending], replacing separate useState flags for loading and errors.
  3. Each input's name attribute (course, comment) maps directly to formData.get(...) on the server.

Practice

  1. Add a numeric rating field (1 to 5) and validate on the server that the score is between 1 and 5.
  2. Call revalidatePath('/') inside your Server Action after saving to a list so the page refreshes automatically.
  3. Display a styled green banner when state.success is true and a red alert when false.

Continue learning: React.js Tutorials and Examples.

References: Next.js Documentation; React useActionState Reference.

logoblog

Thanks for reading Next.js Server Actions for Beginners: Build a Student Feedback Form

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.