Sub topic 10
Using Server Functions as form actions
This chapter assumes you know how to pass a function to <form action>, read values from FormData, and use useActionState. Those were covered earlier in this tutorial. Here you apply them to functions that run on the server.
What a Server Function is
A Server Function is an async function that runs on the server but can be called from client code. You mark it with the "use server" directive. React and your framework handle the rest.
A Server Function is not a new kind of Action. A form's action prop accepts any function, and React runs it as an Action either way. The difference is where the body executes. A client-side Action runs in the browser. A Server Function runs on your server, so it can reach your database or secrets directly.
React 19 does not include a bundler or a server. Server Functions need a framework or bundler that supports them, such as a React Server Components-capable framework. Without one, "use server" does nothing useful.
A basic example
Put the function in a file that starts with "use server". Every export from that file becomes a Server Function.
// actions.js
"use server";
export async function createTodo(formData) {
const title = String(formData.get("title") ?? "").trim();
if (title.length === 0 || title.length > 200) {
throw new Error("Invalid title");
}
// Check that the user is allowed to do this, then write to your data store here.
}Import it and pass it to the form like any other function.
// TodoForm.jsx (a component in a framework that supports Server Functions)
import { createTodo } from "./actions";
export function TodoForm() {
return (
<form action={createTodo}>
<input name="title" />
<button type="submit">Add</button>
</form>
);
}On submit, React collects the form's FormData and sends it to the server. The server runs createTodo and sends the result back. This code never ships to the browser. The client bundle only gets a reference to the function.
What happens on submit
Because the call crosses the network, the usual Action behavior still applies. The transition stays pending until the call finishes, so useFormStatus reports pending while the server works. Uncontrolled inputs reset after a successful Action.
Returning results with useActionState
A thrown error is a poor way to report a mistake the user made, such as a blank field. Return a value instead, and read it with useActionState. The Server Function then takes the previous state as its first argument, followed by FormData.
// actions.js
"use server";
export async function addTodo(previousState, formData) {
const title = String(formData.get("title") ?? "").trim();
if (title.length === 0 || title.length > 200) {
return { error: "Title must be 1 to 200 characters." };
}
// Check that the user is allowed to do this, then write to your data store here.
return { error: null };
}// TodoForm.jsx
"use client";
import { useActionState } from "react";
import { addTodo } from "./actions";
export function TodoForm() {
const [state, formAction, isPending] = useActionState(addTodo, { error: null });
return (
<form action={formAction}>
<input name="title" />
<button type="submit" disabled={isPending}>
Add
</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}The component is a Client Component because it uses a hook. It still imports a Server Function, which is allowed. The return value must be serializable, so return plain objects, strings, and numbers. Do not return class instances or functions.
Common errors
- Putting
"use server"on a Server Component. The directive marks Server Functions. Server Components need no directive. - Calling hooks in the server file. Server Functions run outside rendering, so
useStateand similar hooks are unavailable there. - Returning non-serializable values. The result travels over the network and must be serializable.
- Expecting it to work without framework support. The import will fail or run as ordinary client code.
- Skipping validation because the form already checks. Any client can call a Server Function directly, so it must validate its own input and check permissions.
Exercise
A Server Function is passed to useActionState. What arguments does it receive, and why should it check the input even if the form has client-side validation?
Show answer
It receives the previous state first and the form's FormData second. It must validate on its own because a client can call the function directly and skip the form's checks.