remix-v2-forms

$npx mdskill add existential-birds/beagle/remix-v2-forms

Handle Remix v2 form submissions and mutations with server actions.

  • Implements forms with optimistic UI, file uploads, and multi-action routes.
  • Depends on @remix-run/react and @remix-run/node for form handling.
  • Uses action functions to parse formData and return redirect or json responses.
  • Provides useActionData and useNavigation for error and pending state feedback.

SKILL.md

.github/skills/remix-v2-formsView on GitHub ↗
---
name: remix-v2-forms
description: Remix v2 form submissions and mutations. Use when implementing forms, optimistic UI, file uploads, or multi-action routes. Triggers on <Form>, useFetcher, useSubmit, useNavigation for pending state, unstable_parseMultipartFormData, fetcher.formData, intent-based actions, encType multipart.
---

# Remix v2 Forms & Mutations

Canonical mutation primitives for the `@remix-run/react@^2` route-module
framework. A correct Remix v2 mutation is: a `<Form method="post">` (or
`<fetcher.Form>`), an `action` that parses `request.formData()` and returns
either `redirect(...)` or `json(...)`, and UI that reads `useActionData()`
(or `fetcher.data`) for errors plus `useNavigation()` (or `fetcher.state`)
for pending state. Anything that bypasses this loop — `fetch()`, raw
`<form>`, `e.preventDefault()` + client state — silently sacrifices
revalidation, progressive enhancement, and race-safe transitions.

## Quick Reference

**`<Form>` + action**:

```tsx
import { json, redirect, type ActionFunctionArgs } from "@remix-run/node";
import { Form, useActionData, useNavigation } from "@remix-run/react";

export async function action({ request }: ActionFunctionArgs) {
  const form = await request.formData();
  const email = String(form.get("email") ?? "");
  if (!email.includes("@")) return json({ errors: { email: "Invalid" } }, { status: 400 });
  await createUser({ email });
  return redirect("/dashboard");
}

export default function Signup() {
  const actionData = useActionData<typeof action>();
  const nav = useNavigation();
  const busy = nav.state !== "idle" && nav.formAction === "/signup";
  return (
    <Form method="post" replace>
      <input name="email" type="email" />
      {actionData?.errors?.email ? <em>{actionData.errors.email}</em> : null}
      <button disabled={busy}>{busy ? "Signing up..." : "Sign Up"}</button>
    </Form>
  );
}
```

## Primitives

| Name | Purpose |
|---|---|
| `<Form>` from `@remix-run/react` | Navigating, progressively-enhanced form that posts to a route `action` and triggers full-page revalidation |
| `<Form navigate={false}>` | Shorthand for "post via fetcher; do not navigate." Equivalent to `<fetcher.Form>` without holding a fetcher ref — useful when you only need pending state, not a programmatic handle |
| `useFetcher()` | Non-navigating submission channel for inline mutations, list rows, popovers — same revalidation, no URL change |
| `useFetchers()` | **Read-only** array of all in-flight fetcher states across the app. Use for global pending indicators (top-bar loader) without prop drilling. No `Form`/`submit`/`load` methods on the returned items — just `formData`, `state`, etc. |
| `useNavigation()` | Observes page-level navigation; the source of truth for `<Form>` pending state |
| `useSubmit()` | Programmatic submission (onChange autosave, keyboard shortcuts). Accepts `HTMLFormElement`, `FormData`, plain object (form-encoded), or plain object encoded as JSON via `{ encType: "application/json" }` |
| `useActionData<typeof action>()` | Read the most recent action result for the current route |

State transitions:

- `useNavigation().state`: `idle → submitting → loading → idle` for non-GET
  form submissions; `idle → loading → idle` for GET navigation.
- `useFetcher().state`: `idle → submitting → loading → idle`.

**Asymmetry:** `useNavigation` skips `submitting` for GET navigations; `useFetcher` does NOT — only `fetcher.load()` skips it. `<fetcher.Form method='get'>` and `fetcher.submit(..., {method:'get'})` both transition through `submitting`.

## Key Patterns

### `<Form>` for navigation, `useFetcher` for in-place

`<Form>` changes the URL, adds history, and revalidates all loaders.
`useFetcher` does the same revalidation but stays on the current URL.
Each `useFetcher()` call returns an independent submission channel, so
two rows submitting at once do not share pending state.

### Intent pattern for multiple actions on one route

One `action`, switch on `formData.get("intent")`, distinct
`<button name="intent" value="...">` per operation. Only the clicked
submit button's `name=value` lands in the body. See
[references/intent-actions.md](references/intent-actions.md).

### Optimistic UI from `formData`

`fetcher.formData` and `navigation.formData` are populated synchronously
on submit and cleared at `idle`. Read directly each render; never mirror
into local React state. See
[references/optimistic-ui.md](references/optimistic-ui.md).

### File uploads need `encType="multipart/form-data"`

Without it, `request.formData()` strips file data and you get the
filename string instead of a `File`. Parse with
`unstable_parseMultipartFormData` and a bounded upload handler. The
`unstable_` prefix is permanent in v2. See
[references/uploads.md](references/uploads.md).

## Gates (decision sequencing)

Answer **in order**. **Pass** means the condition is true; pick the API
on the same line and **stop**.

### `<Form>` vs `useFetcher`

1. **Does the URL need to change after the mutation** (creating a record
   and routing to `/records/:id`, deleting and going back to a list,
   multi-step flow)?
   - **Pass →** `<Form method="post">` + `redirect(...)` from the action. **Stop.**
   - **Fail →** Step 2.
2. **Is this a mutation against a row, cell, toggle, or sub-section while
   the user stays on the same page** (favorite, like, increment quantity,
   inline edit)?
   - **Pass →** `useFetcher()` with `<fetcher.Form>`. **Stop.**
   - **Fail →** Step 3.
3. **Is this loading data outside of normal navigation** (popover content,
   combobox results, prefetch)?
   - **Pass →** `fetcher.load(href)`. **Stop.**
   - **Fail →** Default to `<Form>`. Navigation is the conservative
     choice — revalidation and history work out of the box.

Hard rule: never reach for `fetch()` or `axios` for in-app mutations
against your own Remix routes. That bypasses the action lifecycle and
skips loader revalidation.

### `useNavigation` vs `useFetcher.state` for pending state

1. **Is the pending indicator global** (page spinner in root, top-bar
   loading bar)?
   - **Pass →** `useNavigation()` in `root.tsx`
     (`navigation.state !== "idle"`). **Stop.**
   - **Fail →** Step 2.
2. **Was the mutation made with `useFetcher`?**
   - **Pass →** Use that fetcher's `fetcher.state`. `useNavigation()`
     will NOT reflect fetcher activity. **Stop.**
   - **Fail →** Step 3.
3. **Is the indicator scoped to one row/button inside a list where each
   row has its own fetcher?**
   - **Pass →** Use the per-row `fetcher.state` (or look up by key via
     `useFetchers()`) so other rows do not flicker. **Stop.**
   - **Fail →** Step 4.
4. **Is the indicator scoped to the form just submitted via `<Form>`?**
   - **Pass →** `useNavigation()` AND check
     `navigation.formAction === "/expected-path"` so unrelated navigations
     don't trigger your local spinner. **Stop.**
   - **Fail →** Step 5.
5. **Need to render an optimistic value?**
   - **Pass →** Read `navigation.formData?.get("field")` (page form) or
     `fetcher.formData?.get("field")` (fetcher) — both are populated
     while `state !== "idle"`. **Stop.**

## Additional Documentation

- **`<Form>` component**: See [references/form.md](references/form.md) for
  `<Form>` vs native `<form>` vs `fetch()`, progressive enhancement,
  redirect-after-success, and validation error display via `useActionData`.
- **`useFetcher`**: See [references/fetcher.md](references/fetcher.md) for
  inline mutations, list operations, popovers, `fetcher.state`,
  `fetcher.data`, `fetcher.Form`, `fetcher.submit`, `fetcher.load`.
- **Optimistic UI**: See
  [references/optimistic-ui.md](references/optimistic-ui.md) for
  `fetcher.formData` and `useNavigation.formData`, when to apply, and
  reverting on failure.
- **File uploads**: See [references/uploads.md](references/uploads.md)
  for `unstable_parseMultipartFormData`,
  `unstable_createMemoryUploadHandler`,
  `unstable_createFileUploadHandler`, and bounded handlers.
- **Intent-based actions**: See
  [references/intent-actions.md](references/intent-actions.md) for
  multiple actions on one route via the FormData `intent` field.

## Comparison

| Concern | `<Form>` | `useFetcher` | Native `<form>` | `fetch()` |
|---|---|---|---|---|
| URL change / history entry | Yes | No | Yes (hard nav) | No |
| Works without JS | Yes | Yes | Yes | No |
| Revalidates loaders | Yes | Yes | Yes (hard reload) | No |
| Pending state hook | `useNavigation()` | `fetcher.state` | None | Manual |
| Optimistic input source | `navigation.formData` | `fetcher.formData` | None | Manual |
| In-app mutation use case | Create / delete / multi-step | Inline / row / toggle | External targets only | Never for own routes |

More from existential-birds/beagle

SkillDescription
adr-decision-extractionUse when you need to mine a conversation, session transcript, or design discussion for architectural decisions before writing ADRs. Identifies problem-solution pairs, trade-off debates, technology choices, and explicit \"[ADR]\" tags. Triggers on \"what decisions did we make\", \"extract decisions from this chat\", \"find the choices in our discussion\", or \"summarize architectural decisions\". Also useful after long planning sessions to capture decisions that were made implicitly. Does NOT write ADR documents \u2014 use adr-writing or write-adr for that.
adr-writingUse when writing or formatting an ADR document using the MADR template, applying Definition of Done (E.C.A.D.R.) criteria, or verifying ADR completeness. Triggers on \"write the ADR\", \"format as MADR\", \"check ADR quality\", \"mark gaps in ADR\". Also triggers when a decision has been extracted and needs to become a document. Does NOT extract decisions from conversations (use adr-decision-extraction) or orchestrate the full extract-confirm-write workflow (use write-adr).
agent-architecture-analysisUse when auditing an agent codebase against the 12-Factor Agents methodology, reviewing LLM-powered system architecture, or assessing agentic app compliance. Triggers on \"analyze agent architecture\", \"12-factor audit\", \"how compliant is this agent\", or \"evaluate this LLM app\". Also applies when comparing frameworks or planning agent improvements. Not for quick checklists \u2014 this performs deep per-factor codebase analysis with file-level evidence.
app-intents-code-reviewReviews App Intents code for intent structure, entities, shortcuts, and parameters. Use when reviewing code with import AppIntents, @AppIntent, AppEntity, AppShortcutsProvider, or @Parameter.
artifact-analysisUse when the user wants a cited, structured read of local documents and project knowledge. Triggers on: \"analyze these docs\", \"scan my project for context\", \"read the docs folder\", \"summarize what's in .beagle/concepts/\", \"extract context from docs/\", \"what's in this folder\", \"go read everything in X and tell me what's there\". Also invoked programmatically by other beagle skills (prfaq-beagle Ignition, brainstorm-beagle reference points, strategy-interview context grounding) via the companion contract. Does NOT trigger on codebase lookups (\"find this function\", \"search the repo\"), web research (use web-research), LLM-as-judge evaluation (use llm-judge), or document editing (use humanize-beagle). Produces a written scan plan, parallel-subagent findings, and a cited synthesis report on disk — never inline prose, never unsourced claims.
axum-code-reviewReviews axum web framework code for routing patterns, extractor usage, middleware, state management, and error handling. Use when reviewing Rust code that uses axum, tower, or hyper for HTTP services. Covers axum 0.7+ patterns including State, Path, Query, Json extractors.
brainstorm-beagleUse when the user has a fuzzy idea and wants to shape it into a concrete project spec before planning or building. Triggers on: \"brainstorm this\", \"I have an idea for...\", \"help me think through this project\", \"what should I build\", \"spec this out\". Also catches vague feature descriptions needing structured questioning to clarify scope. Does NOT write code, plan implementation, review strategy docs, or run strategy interviews \u2014 produces a WHAT/WHY spec through dialogue, not a HOW plan.
bubbletea-code-reviewReviews BubbleTea TUI code for proper Elm architecture, model/update/view patterns, and Lipgloss styling. Use when reviewing terminal UI code using charmbracelet/bubbletea.
cloudkit-code-reviewReviews CloudKit code for container setup, record handling, subscriptions, and sharing patterns. Use when reviewing code with import CloudKit, CKContainer, CKRecord, CKShare, or CKSubscription.
combine-code-reviewReviews Combine framework code for memory leaks, operator misuse, and error handling. Use when reviewing code with import Combine, AnyPublisher, @Published, PassthroughSubject, or CurrentValueSubject.