Server Actions in Forms: The action Prop
You'll learn how to connect Server Actions directly to HTML forms using the action prop for automatic progressive enhancement.
The Built-In Integration
Instead of manually calling a Server Action from an onSubmit handler, you can pass it directly to a form's action prop. This looks like regular HTML but unlocks powerful behavior:
// Server Component
async function createUser(formData: FormData) {
'use server'
const name = formData.get('name')
// Save to database...
}
export default function SignupForm() {
return (
<form action={createUser}>
<input name="name" required />
<button type="submit">Sign Up</button>
</form>
)
}
Progressive Enhancement Magic
Here's what makes this special: the form works even if JavaScript fails to load. When a user submits:
- Without JS: The browser does a traditional form POST. React intercepts it server-side and runs your action.
- With JS: React prevents the default submission, calls your Server Action seamlessly, and updates the UI without a full page reload.
You write the code once and get both experiences automatically.
The FormData Parameter
Your Server Action receives a native FormData object. Use .get('fieldName') to extract values—no event objects, no e.preventDefault(), just the form data.
Think of it like this: you're writing old-school server-side form handling, but React makes it feel modern with client-side interactivity as a bonus layer.
Key Takeaway: Passing a Server Action to action gives you a form that works without JavaScript and enhances automatically when JS loads—the best of both worlds with zero extra code.