FormData in Server Actions
What you'll learn: How to extract and work with form field values inside server actions using the native FormData API.
The Core Idea
When a form submits to a server action, React automatically packages all the form field values into a FormData object and passes it as the first argument to your server action function. This is the same native browser FormData API you may have used before, giving you straightforward access to every input, textarea, and select value.
Think of FormData as a labeled envelope system: each form field's name attribute becomes the label, and its value becomes the contents. Your server action opens these envelopes to read what the user submitted.
Accessing Form Values
'use server'
export async function createPost(formData: FormData) {
// Get individual field values by their name attribute
const title = formData.get('title');
const content = formData.get('content');
const published = formData.get('published');
console.log(title); // "My First Post"
console.log(published); // "on" (checkbox) or null
}
The formData.get(name) method returns the value as a string (or null if the field doesn't exist). For multi-select fields or multiple files, use formData.getAll(name) to get an array.
Remember: the name attribute in your JSX must match the string you pass to .get(). No name, no data.
Why FormData?
This approach mirrors how traditional forms worked before JavaScript frameworks—simple, reliable, and progressively enhanced. Your form works even if JavaScript fails to load, because FormData is built into the browser.
Key Takeaway: Server actions receive form submissions as a FormData object, letting you extract values using .get(fieldName) where the field name matches the input's name attribute.