Course contentsShow
← React
Lesson 1602 of 2,39639. Server Components in PracticeFree lesson

Reading FormData in Server Actions

Server Actions receive FormData as the first argument, letting you access fields with formData.get('fieldName').

Reading FormData in Server Actions

You'll learn how to access form field values inside a Server Action using the FormData object.

The First Argument is Always FormData

When you attach a Server Action to a form's action prop, React automatically passes the submitted form data as the first argument to your function. This argument is a standard web FormData object, containing all the field values from your form.

Think of it like a package delivery: when the form submits, React packages up all the input fields into a FormData container and hands it to your Server Action function on the server.

Reading Individual Fields

To extract a specific field's value, use formData.get('fieldName'), where the string matches the name attribute of your form input:

// Server Component
async function createPost(formData) {
  'use server'
  
  const title = formData.get('title')
  const content = formData.get('content')
  
  console.log('Title:', title)
  console.log('Content:', content)
  
  // Now you can use these values to save to a database, etc.
}

export default function PostForm() {
  return (
    <form action={createPost}>
      <input name="title" type="text" />
      <textarea name="content" />
      <button type="submit">Create Post</button>
    </form>
  )
}

The name attribute on each input is crucial—it's the key you'll use with .get() to retrieve that field's value.

What You Get Back

formData.get() returns the field's value as a string (or null if the field doesn't exist). For multiple fields with the same name, use formData.getAll('fieldName') to receive an array of all values.

Key Takeaway: Server Actions receive FormData as their first parameter, and you access field values with formData.get('fieldName') using the input's name attribute as the key.