← 返回
未分类

Remix V2 Forms

Remix v2 form submissions and mutations. Use when implementing forms, optimistic UI, file uploads, or multi-action routes. Triggers on <Form>, useFetcher, us...
Remix v2 表单提交与变更。在实现表单、乐观 UI、文件上传或多操作路由时使用。触发于 <Form>、useFetcher、us...
anderskev
未分类 clawhub v1.0.0 1 版本 99543.4 Key: 无需
★ 0
Stars
📥 218
下载
💾 1
安装
1
版本
#latest

概述

Remix v2 Forms & Mutations

Canonical mutation primitives for the @remix-run/react@^2 route-module

framework. A correct Remix v2 mutation is: a

(or

), 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

, e.preventDefault() + client state — silently sacrifices

revalidation, progressive enhancement, and race-safe transitions.

Quick Reference

+ action:

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

NamePurpose
------
from @remix-run/reactNavigating, progressively-enhanced form that posts to a route action and triggers full-page revalidation
Shorthand for "post via fetcher; do not navigate." Equivalent to 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 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()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. and fetcher.submit(..., {method:'get'}) both transition through submitting.

Key Patterns

for navigation, useFetcher for in-place

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

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-23 23:45 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

Vitest Testing

anderskev
Vitest 测试框架模式与最佳实践。适用于编写单元测试、集成测试、配置 vitest.config、使用 vi.mock/vi.fn 进行模拟等...
★ 0 📥 923

Rust Code Review

anderskev
审查 Rust 代码,涵盖所有权、借用、生命周期、错误处理、trait 设计、unsafe 使用及常见错误,适用于 .rs 文件审查,检查...
★ 0 📥 767

Rust Testing Code Review

anderskev
审查 Rust 测试代码,包括单元测试模式、集成测试结构、异步测试、模拟方式和属性测试,覆盖 Rust 2024 版。
★ 0 📥 782