Sub topic 12
Server functions and progressive enhancement
This chapter assumes you know React function components, the useActionState signature, and how an action function returns the next state from prevState and FormData. It covers what changes when the action is a server function.
What a server function is
A server function is an async function that runs on the server but is called from client code. You mark it with the "use server" directive. The framework (for example a Next.js or other React Server Components setup) turns a call to it into a network request. Plain client-only React setups, such as a Vite app, do not provide this.
You can pass a server function to useActionState as the action. The contract stays the same: it receives the previous state and the FormData, and returns the next state.
// actions.js
"use server";
export async function createPost(prevState, formData) {
const title = String(formData.get("title") ?? "").trim();
if (!title) {
return { error: "Title is required.", title };
}
// save the post here
return { error: null, title: "" };
}The returned state must be serializable, because it travels from the server to the browser. Plain objects, arrays, strings, numbers, and booleans work. Functions and class instances do not.
The permalink argument
Normally a form needs JavaScript to submit through useActionState. If the page is server-rendered and the script has not loaded yet, a submit would fall back to a plain browser form post with nowhere useful to go.
The optional third argument, the permalink, fixes this. It is the URL of the page that owns the form's result. If the user submits before hydration finishes, the browser posts the form to that URL. The server runs the action, then renders that page with the resulting state.
// PostForm.jsx
"use client";
import { useActionState } from "react";
import { createPost } from "./actions";
export function PostForm() {
const [state, formAction, isPending] = useActionState(
createPost,
{ error: null, title: "" },
"/posts/new"
);
return (
<form action={formAction}>
<input name="title" type="text" defaultValue={state.title} />
<button type="submit" disabled={isPending}>Create</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}This is progressive enhancement. The form works as plain HTML first, and JavaScript improves it once it arrives. The defaultValue={state.title} line matters here. Without it, a server-rendered result would show an empty field after a failed submit.
Rules for the permalink
- The action must be a server function. A client-side function cannot run before JavaScript loads.
- The permalink must point to a page that renders the same form with the same action. If the pages differ, the state the server returns has no matching form.
- If the form uses a different action on the target page, React cannot carry the state across.
- Without a framework that supports server functions, the argument has no effect.
Trust and validation
A server function is a public endpoint. Anyone can send it a request, not only your form. Validate the input again on the server and check authorization inside the function, even if the client already checked the same things.
Exercise
A server-rendered form uses useActionState with a server function and no permalink. A user submits it before the JavaScript bundle loads. Why is the result unreliable, and what change fixes it?
Show answer
Without a permalink, React has no URL to post the form to before hydration, so the submission has no page to return the result to. Pass the page's URL as the third argument to useActionState, for example "/posts/new". The browser then posts to that URL, the server runs the action, and it renders the page with the returned state. Also use defaultValue={state.title} so the typed value shows again.