Sub topic 11
Forms that work before JavaScript loads
This chapter assumes you know useActionState, the form action prop, and FormData from the earlier chapters. It also assumes you have a framework that supports Server Functions, such as Next.js with the App Router. Plain client-side React cannot do what this chapter describes.
Why this matters
A browser can submit a <form> with no JavaScript at all. It sends an HTTP request, and the server responds with a new page. Building on that is called progressive enhancement: the form works first as plain HTML, and JavaScript then improves it.
JavaScript can be late or missing. The bundle may still be downloading on a slow network. A script may fail to load, or the user may have blocked it. If your form depends on JavaScript, clicking Submit during that window does nothing useful.
Where the function lives decides the behavior
A function you pass to action can live in two places.
| Where the function is defined | Works before JavaScript loads? | Why |
|---|---|---|
| Inside a client component (a plain function) | No | The function does not exist in the browser until the bundle runs. |
| In a `"use server"` file (a Server Function) | Yes, with framework support | The framework can render the form so the browser posts it to the server, which runs the function. |
With a Server Function, the framework gives the form an endpoint to post to. If JavaScript has not loaded, the browser does an ordinary full-page POST. After hydration, React intercepts the submit and calls the function without reloading the page.
Example: a signup form with state
Define the Server Function in its own file. It takes the previous state and the FormData, as in the earlier useActionState chapter.
// actions.js
"use server";
export async function signup(previousState, formData) {
const email = String(formData.get("email") ?? "").trim();
if (!email.includes("@")) {
return { error: "Enter a valid email address.", email };
}
// Save the email to your data store here.
return { error: null, email };
}Then wire it up with useActionState in a client component.
// SignupForm.jsx
"use client";
import { useActionState } from "react";
import { signup } from "./actions";
export function SignupForm() {
const [state, formAction, isPending] = useActionState(
signup,
{ error: null, email: "" },
"/signup"
);
return (
<form action={formAction}>
<input name="email" type="email" defaultValue={state.email} />
<button type="submit" disabled={isPending}>
Sign up
</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}The third argument to useActionState is the optional permalink. It is the URL of the page that contains this form. If the user submits before hydration, the framework uses it to know which page to show with the returned state. Without a permalink, a pre-hydration submit still reaches the Server Function, but the resulting page may not be the one the form lives on. The permalink only matters when the action is a Server Function.
Rules that keep the form working
- Use
nameattributes on every input. Without JavaScript, the browser builds theFormDatafrom them. - Keep state in the form, not in
useState. Controlled inputs depend on JavaScript to hold their values, so usedefaultValueas above. - Validate on the server. The Server Function is the only code guaranteed to run.
- Keep client-only features as enhancements.
useOptimisticandisPendingneed JavaScript. The form should still submit and show the result without them. - Use a real
<button type="submit">. A button with only anonClickhandler does nothing without JavaScript.
Common errors
- Passing a client function to
actionand expecting it to work early. It cannot. Move the logic into a Server Function. - Using a framework that does not support Server Functions. The code may compile, but no endpoint exists for the pre-hydration submit.
- Relying on controlled inputs. After a full-page POST, the browser has no JavaScript state, so the field comes back empty unless you return the value in state and set
defaultValue. - Forgetting the permalink in a multi-route app. The result can render on an unexpected page.
Exercise
A form uses action={saveDraft}, where saveDraft is an async function defined inside the client component. A user on a slow connection clicks Submit before the JavaScript bundle loads. What happens, and what change would let that submit work?
Show answer
Nothing useful happens, because saveDraft does not exist in the browser until the bundle runs. Move the logic into a "use server" file and import it, in a framework that supports Server Functions. The form can then post to the server without JavaScript. If you use useActionState, also pass the page's URL as the permalink and return the submitted values in state so the page renders correctly after the POST.