Sub topic 7
Using Server Functions as form actions
This chapter assumes you know React components, props and hooks, async/await, and how HTML forms and FormData work. It also assumes you have read the earlier chapters on the "use server" directive and on defining Server Functions.
Functions in the `action` prop
In plain HTML, action is a URL. React 19 also accepts a function there. When the user submits the form, React stops the browser's default navigation and calls your function with a FormData object. The object holds every named field in the form.
If the function is a Server Function, React sends the FormData to the server, runs the function there, and returns the result to the client.
The simplest form action
You can define the function inline in a Server Component. The function-level 'use server' directive marks it as a Server Function.
// app/page.js (a Server Component)
import { db } from './db';
export default function Page() {
async function createNote(formData) {
'use server';
await db.notes.insert({ title: formData.get('title') });
}
return (
<form action={createNote}>
<input name="title" required />
<button type="submit">Add note</button>
</form>
);
}The name attribute on the input decides the key that formData.get('title') reads. An input without a name is not included in FormData.
Importing the function from a file
Client Components cannot define Server Functions inline. They import them from a file that starts with 'use server'.
// app/actions.js
'use server';
import { db } from './db';
export async function createNote(formData) {
const title = formData.get('title');
await db.notes.insert({ title });
}Any Server or Client Component can import createNote and pass it to action. The same file can hold many exported Server Functions.
Progressive enhancement
If a Server Component renders the form and passes it a Server Function, the form can submit before the page's JavaScript has loaded. The browser sends an ordinary form POST, and the framework runs the function. Once JavaScript loads, React takes over and submits without a full page reload.
This requires a framework or bundler that supports it. It also depends on where the form lives. A form inside a Client Component can only submit as a plain POST before hydration if you use useActionState with a permalink, which this chapter does not cover. Without one, React queues the submission until hydration finishes.
Behavior to know
- Arguments. The function receives only
FormData. Read values withget, and convert them yourself.getreturns a string or aFile, never a number. - Trust.
FormDatacomes from the client. A user can send any fields, so validate every value. Treat the function as a public endpoint. - Return values. A plain
actionignores the return value. To show validation messages, useuseActionState. - Refreshing data. A mutation does not update the page by itself. Your framework provides a way to revalidate data after the function runs.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Inline `'use server'` function inside a Client Component | Build error | Move it to a `'use server'` file and import it |
| Input with no `name` | `formData.get` returns `null` | Add a `name` attribute |
| Using `formData.get('age')` as a number | You get a string | Convert and validate it |
| Skipping validation because the form has `required` | Direct calls bypass the browser check | Validate on the server |
Exercise
A form passes a Server Function to action, but formData.get('title') always returns null. The input is <input id="title" />. What is wrong, and why does a required attribute on the input not make the function safe?
Show answer
The input has an id but no name, and FormData is keyed by name, so the field is never included. Add name="title". The required attribute only affects the browser's own form check. A client can call a Server Function directly with any data, so the function must validate its input itself.