Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 39 ยท Actions and Forms

The Form Component and Progressive Enhancement

Use the React Router v7 Form component for GET and POST forms, read actionData, show pending state with useNavigation, and keep forms working without JavaScript.

React Router's <Form> looks like a normal HTML form, because it is one. It renders a real <form>, so it works before JavaScript loads and with JavaScript turned off. When the app is hydrated, the router intercepts the submit, sends it with fetch, runs the action, and updates the page without a reload. This is progressive enhancement: a working baseline, made nicer by JavaScript.

The framework cannot run here, so the TSX is read-only; the runnable stand-ins model what the router does around a submission.

Form props that matter

TSX
import { Form } from "react-router";

<Form method="post" action="/notes/new" replace>
  ...
</Form>
  • method: get (runs the loader with search params), or post/put/patch/delete (runs the action).
  • action: the URL to submit to. Omit it to submit to the current route. This is the usual choice and keeps the form working inside nested layouts.
  • replace: replace the history entry instead of pushing a new one.
  • reloadDocument: do a normal full-page submission, skipping the router.
  • navigate={false}: submit without navigating (use a fetcher for that, see Fetchers).

Reading the result with actionData

Whatever the action returns (other than a redirect) arrives as actionData:

TSX
export async function action({ request }: Route.ActionArgs) {
  const form = await request.formData();
  const email = String(form.get("email") ?? "");
  if (!email.includes("@")) {
    return { error: "Enter a valid email address", values: { email } };
  }
  await subscribe(email);
  return { success: true };
}

export default function Newsletter({ actionData }: Route.ComponentProps) {
  return (
    <Form method="post">
      <input type="email" name="email" defaultValue={actionData?.values?.email} />
      {actionData?.error && <p role="alert">{actionData.error}</p>}
      {actionData?.success && <p role="status">Thanks, you are subscribed.</p>}
      <button>Subscribe</button>
    </Form>
  );
}

Returning values lets the form refill what the user typed, which matters for the no-JavaScript path: a full-page POST rerenders from scratch, and uncontrolled inputs would otherwise be blank.

JS Stand-in: an action that returns errors and echoed values

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Pending UI with useNavigation

While a submission runs, useNavigation() reports state: "submitting" and exposes formData, formMethod and formAction of the in-flight submit.

TSX
import { Form, useNavigation } from "react-router";

function SaveForm() {
  const navigation = useNavigation();
  const saving = navigation.state === "submitting";
  return (
    <Form method="post">
      <fieldset disabled={saving}>
        <input name="title" />
        <button>{saving ? "Saving..." : "Save"}</button>
      </fieldset>
    </Form>
  );
}

Disabling the fieldset prevents double submissions. (The values of disabled controls are not sent, but the submission has already been captured by then.)

JS Stand-in: navigation state during a submission

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the three phases of a submission: submitting (action running), loading (loaders rerunning after the action), then idle.

GET forms are navigation

A method="get" form never runs an action. It turns the fields into search params and navigates, so the loader runs with the new URL. Use it for search and filters (see Request, Params and Search Params in Loaders).

Making forms work without JavaScript

Test by disabling JavaScript in your browser. A well-built form needs no extras:

  • Use real <button type="submit">, not div onClick.
  • Give inputs name attributes: FormData only contains named fields.
  • Do not rely on useState to hold values that must be submitted.
  • Return errors from the action and render them, instead of toasts triggered in event handlers.

Where JavaScript adds value, layer it on top: pending states, optimistic UI (Optimistic UI) and focus handling.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens to a <Form> when JavaScript fails to load?

    Form renders a real form element, so the browser submits it natively.
  2. 2. If action is omitted on <Form>, where does it submit?

    The default is the route that renders the form.
  3. 3. Which navigation.state occurs while the action is running?

    After the action, state becomes loading while loaders rerun.
  4. 4. Why return the submitted values from an action on validation errors?

    Without JavaScript the page reloads and uncontrolled inputs would be empty.

Summary

  • <Form> is a real form enhanced by the router; default action is the current route.
  • Read results through actionData and echo submitted values on errors.
  • useNavigation shows submitting, then loading, then idle.
  • GET forms navigate; non-GET forms run actions.
  • Build for the no-JavaScript path first, then enhance.

Next: Validation and Form Errors