Skip to content

FlashcardNot attempted

A Server Action end to end

Recall it before you reveal

Write a Server Action that saves a form and refreshes the page data, and say how to show a pending state.

Space to reveal
Show answer & explanation
'use server';
import { revalidatePath } from 'next/cache';
export async function submitFeedback(prevState: unknown, formData: FormData) {
const feedback = formData.get('feedback');
if (!feedback) return { error: 'Feedback is required' };
await db.feedback.create({ data: { feedback: String(feedback) } });
revalidatePath('/product/[slug]', 'page');
return { success: true };
}

Because it returns a result, wire it through useActionState: const [state, formAction, isPending] = useActionState(submitFeedback, null), then <form action={formAction}> and render state?.error. (A plain <form action={fn}> discards the return value.) For a pending submit button, useFormStatus() in a child component.