jpskill.com
🛠️ 開発・MCP コミュニティ

web-forms-react-hook-form

React Hook Formというライブラリを活用し、フォームの作成、入力制御、配列データの扱い、バリデーション設定、パフォーマンス改善など、Reactで効率的なWebフォームを構築・最適化するSkill。

📜 元の英語説明(参考)

React Hook Form patterns - useForm, Controller, useFieldArray, validation resolver, performance optimization

🇯🇵 日本人クリエイター向け解説

一言でいうと

React Hook Formというライブラリを活用し、フォームの作成、入力制御、配列データの扱い、バリデーション設定、パフォーマンス改善など、Reactで効率的なWebフォームを構築・最適化するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o web-forms-react-hook-form.zip https://jpskill.com/download/10279.zip && unzip -o web-forms-react-hook-form.zip && rm web-forms-react-hook-form.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10279.zip -OutFile "$d\web-forms-react-hook-form.zip"; Expand-Archive "$d\web-forms-react-hook-form.zip" -DestinationPath $d -Force; ri "$d\web-forms-react-hook-form.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して web-forms-react-hook-form.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → web-forms-react-hook-form フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

React Hook Form のパターン

クイックガイド: ネイティブの入力には register を、制御されたコンポーネントには Controller を、動的なフィールドには useFieldArray を使用します。常に useForm<FormData>() ジェネリクスを提供してください。最適な UX のために mode: "onBlur" を設定します。スキーマ検証には resolver パターンを使用します。フォーム全体の再レンダリングを避けるために、レンダリング内で watch() の代わりに useWatch を使用します。useFieldArray ではキーとして field.id を使用します。配列のインデックスは絶対に使用しないでください。


<critical_requirements>

重要: この Skill を使用する前に

すべてのコードは、CLAUDE.md のプロジェクト規約に従う必要があります (kebab-case、名前付きエクスポート、インポート順序、import type、名前付き定数)

(型安全なフォーム処理のために、useForm<FormData>() にジェネリック型を必ず指定する必要があります)

(useFieldArray では、キーの prop として field.id を必ず使用してください - 配列のインデックスは絶対に使用しないでください)

(ref を公開しない制御されたコンポーネントには、必ず Controller を使用してください)

(スキーマ検証には、必ず resolver パターンを使用してください - スキーマをフォームロジックから分離してください)

(最適な UX のために、mode: "onBlur" または mode: "onTouched" を設定してください - 必要な場合を除き、mode: "onChange" は避けてください)

</critical_requirements>


自動検出: React Hook Form, useForm, register, handleSubmit, formState, Controller, useFieldArray, useWatch, useFormContext, resolver, zodResolver, FormProvider, useFormState, FormStateSubscribe

使用する場面:

  • 検証要件のあるフォームを構築する場合
  • 多数のフィールドを持つ複雑なフォームの状態を管理する場合
  • フィールドの追加/削除が可能な動的なフォームを作成する場合
  • 制御されたコンポーネントライブラリと統合する場合
  • 複数ステップまたはウィザード形式のフォームを処理する場合

使用しない場面:

  • 検証のない単一の入力 (useState を使用)
  • サーバーアクションを使用するサーバー専用フォーム (ネイティブフォーム + action を使用)
  • 読み取り専用のデータ表示 (フォームのシナリオではない)

カバーする主要なパターン:

  • TypeScript ジェネリクスを使用した useForm フック
  • register と Controller の使い分け
  • 動的なフィールドのための useFieldArray
  • スキーマ検証のための Resolver の統合
  • パフォーマンスのための useWatch と useFormState
  • ネストされたコンポーネントのための FormProvider/useFormContext
  • フォームのリセット、非同期データロード、および values prop
  • ターゲットを絞った再レンダリングのための FormStateSubscribe (v7.68+)

<philosophy>

Philosophy

React Hook Form は、非制御入力とサブスクリプションベースの更新を通じてパフォーマンスを優先します。変更されたフィールドのみが再レンダリングされ、フォーム全体は再レンダリングされません。このライブラリは、フォームの状態をコンポーネントの状態から分離し、再レンダリングを最小限に抑え、多数のフィールドがあってもフォームの応答性を維持します。

コア原則:

  1. デフォルトで非制御 - 再レンダリングを避けるために、ネイティブの入力には register を使用します
  2. 必要な場合に制御 - ref を公開しない UI ライブラリコンポーネントには Controller を使用します
  3. resolver を介したスキーマ検証 - 検証ロジックをフォームロジックから分離します
  4. サブスクリプションベース - 必要なフォームの状態のみをサブスクライブします (useWatchuseFormState)
  5. 型安全性 - フォームデータには常に TypeScript ジェネリクスを提供します

</philosophy>


<patterns>

コアパターン

パターン 1: TypeScript を使用した基本的な useForm

常に型パラメータ、mode、および defaultValues を指定してください。これら 3 つは、最も一般的な問題 (型安全性の欠如、検証ノイズ、未定義の警告) を防ぎます。

const {
  register,
  handleSubmit,
  formState: { errors, isSubmitting },
} = useForm<ContactFormData>({
  mode: "onBlur",
  defaultValues: { name: "", email: "", message: "" },
});

これが重要な理由: ジェネリクスがない場合、フィールド名は any になります。defaultValues がない場合、値は undefined になり、ハイドレーションの不一致が発生します。mode: "onBlur" がない場合、デフォルトの "onSubmit" では、最初の送信までフィードバックが得られません。

アクセシビリティ属性とエラー表示を備えた完全なフォームについては、examples/core.md を参照してください。


パターン 2: 制御されたコンポーネントのための Controller

コンポーネントがネイティブの ref を公開しない場合 (カスタムセレクト、日付ピッカー、リッチテキストエディタ) は Controller を使用します。標準の HTML 入力には register を使用します。

<Controller
  name="service"
  control={control}
  rules={{ required: "Service is required" }}
  render={({ field, fieldState: { error } }) => (
    <>
      <Select {...field} options={serviceOptions} />
      {error && <span role="alert">{error.message}</span>}
    </>
  )}
/>

重要な判断: コンポーネントがネイティブ入力に転送される ref prop を受け入れる場合、register が機能します。それ以外の場合は、Controller を使用します。

シングルセレクト、日付ピッカー、およびマルチセレクトチェックボックスのパターンについては、examples/controlled-components.md を参照してください。


パターン 3: 動的なフィールドのための useFieldArray

繰り返し可能なフィールドグループには useFieldArray を使用します。React のキーとして常に field.id を使用してください -- 配列のインデックスは、追加/削除時に状態の破損を引き起こします。

const { fields, append, remove } = useFieldArray({ control, name: "items" });

{fields.map((field, index) => (
  <div key={field.id}> {/* 重要: field.id、インデックスは絶対に使用しない */}
    <input {...register(`items.${index}.name`)} />
    <button type="button" onClick={() => remove(index)}>Remove</button>
  </div>
))}

注意: append / prepend / insert には、(部分的なものではなく) 完全なオブジェクトが必要です。最小アイテム検証には、useFieldArrayrules.minLength を使用します。配列レベルのエラーは errors.items.root に存在します。

計算された合計を含む完全な請求書フォームについては、examples/arrays.md を参照してください。


パターン 4: スキーマ検証のための Resolver

検証スキーマを統合するには resolver を使用します。resolver は検証を処理します。それをフォームに接続します。スキーマ定義をフォームコードから分離します。

import { zodResolver } from "@hookform/resolvers/zod";

const { register, handleSubmit } = useForm<FormData>({
  resolver: zodResolver(schema),
  mode: "onBlur",
  defaultValues: { username: "", email: "" },
});

インラインルールよりも resolver が優れている理由: スキーマは独立してテスト可能で、フォーム間で再利用可能で、フィールド間の検証 (例: confirmPassword) をサポートし、z.infer を介して TypeScript 型を生成します。

参照

(原文がここで切り詰められています)

📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

React Hook Form Patterns

Quick Guide: Use register for native inputs, Controller for controlled components, useFieldArray for dynamic fields. Always provide useForm<FormData>() generics. Set mode: "onBlur" for optimal UX. Use resolver pattern for schema validation. Use useWatch instead of watch() in render to avoid re-rendering the whole form. Use field.id as key in useFieldArray -- never array index.


<critical_requirements>

CRITICAL: Before Using This Skill

All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering, import type, named constants)

(You MUST provide generic types to useForm<FormData>() for type-safe form handling)

(You MUST use field.id as key prop in useFieldArray - NEVER use array index)

(You MUST use Controller for controlled components that don't expose a ref)

(You MUST use resolver pattern for schema validation - keep schemas separate from form logic)

(You MUST set mode: "onBlur" or mode: "onTouched" for optimal UX - avoid mode: "onChange" unless needed)

</critical_requirements>


Auto-detection: React Hook Form, useForm, register, handleSubmit, formState, Controller, useFieldArray, useWatch, useFormContext, resolver, zodResolver, FormProvider, useFormState, FormStateSubscribe

When to use:

  • Building forms with validation requirements
  • Managing complex form state with many fields
  • Creating dynamic forms with add/remove fields
  • Integrating with controlled component libraries
  • Handling multi-step or wizard forms

When NOT to use:

  • Single input without validation (use useState)
  • Server-only forms with server actions (use native form + action)
  • Read-only data display (not a form scenario)

Key patterns covered:

  • useForm hook with TypeScript generics
  • register vs Controller decision
  • useFieldArray for dynamic fields
  • Resolver integration for schema validation
  • useWatch and useFormState for performance
  • FormProvider/useFormContext for nested components
  • Form reset, async data loading, and values prop
  • FormStateSubscribe for targeted re-renders (v7.68+)

<philosophy>

Philosophy

React Hook Form prioritizes performance through uncontrolled inputs and subscription-based updates. Only fields that change re-render, not the entire form. The library isolates form state from component state, minimizing re-renders and keeping forms responsive even with many fields.

Core Principles:

  1. Uncontrolled by default - Use register for native inputs to avoid re-renders
  2. Controlled when needed - Use Controller for UI library components that don't expose a ref
  3. Schema validation via resolver - Separate validation logic from form logic
  4. Subscription-based - Subscribe to only the form state you need (useWatch, useFormState)
  5. Type safety - Always provide TypeScript generics for form data

</philosophy>


<patterns>

Core Patterns

Pattern 1: Basic useForm with TypeScript

Always provide a type parameter, mode, and defaultValues. These three prevent the most common issues (no type safety, validation noise, undefined warnings).

const {
  register,
  handleSubmit,
  formState: { errors, isSubmitting },
} = useForm<ContactFormData>({
  mode: "onBlur",
  defaultValues: { name: "", email: "", message: "" },
});

Why this matters: Without generics, field names are any. Without defaultValues, values are undefined and cause hydration mismatches. Without mode: "onBlur", the default "onSubmit" gives no feedback until first submit.

See examples/core.md for complete form with accessibility attributes and error display.


Pattern 2: Controller for Controlled Components

Use Controller when a component doesn't expose a native ref (custom selects, date pickers, rich text editors). Use register for standard HTML inputs.

<Controller
  name="service"
  control={control}
  rules={{ required: "Service is required" }}
  render={({ field, fieldState: { error } }) => (
    <>
      <Select {...field} options={serviceOptions} />
      {error && <span role="alert">{error.message}</span>}
    </>
  )}
/>

Key decision: If the component accepts a ref prop that forwards to a native input, register works. Otherwise, use Controller.

See examples/controlled-components.md for single select, date picker, and multi-select checkbox patterns.


Pattern 3: useFieldArray for Dynamic Fields

Use useFieldArray for repeatable field groups. Always use field.id as the React key -- array index causes state corruption on add/remove.

const { fields, append, remove } = useFieldArray({ control, name: "items" });

{fields.map((field, index) => (
  <div key={field.id}> {/* CRITICAL: field.id, never index */}
    <input {...register(`items.${index}.name`)} />
    <button type="button" onClick={() => remove(index)}>Remove</button>
  </div>
))}

Gotcha: append/prepend/insert require complete objects (not partial). Use rules.minLength on useFieldArray for minimum item validation. Array-level errors live at errors.items.root.

See examples/arrays.md for a complete invoice form with calculated totals.


Pattern 4: Resolver for Schema Validation

Use resolver to integrate validation schemas. The resolver handles validation; you wire it to the form. Keep schema definition separate from form code.

import { zodResolver } from "@hookform/resolvers/zod";

const { register, handleSubmit } = useForm<FormData>({
  resolver: zodResolver(schema),
  mode: "onBlur",
  defaultValues: { username: "", email: "" },
});

Why resolver over inline rules: Schemas are testable independently, reusable across forms, support cross-field validation (e.g. confirmPassword), and generate TypeScript types via z.infer.

See examples/validation.md for resolver integration with a registration form.


Pattern 5: useWatch for Reactive Derived Values

Use useWatch in a separate component to subscribe to specific fields without re-rendering the entire form. Prefer useWatch over watch() in render.

function PriceDisplay({ control }: { control: Control<PricingFormData> }) {
  const [plan, seats] = useWatch({ control, name: ["plan", "seats"] });
  return <div>Total: ${PLAN_PRICES[plan] * seats}</div>;
}

v7.61+ compute option: Transform watched values before subscription -- component only re-renders when the computed result changes.

const total = useWatch({
  control,
  compute: ({ plan, seats, billingCycle }) => {
    const base = PLAN_PRICES[plan] * seats;
    return billingCycle === "annual" ? base * 12 * (1 - ANNUAL_DISCOUNT) : base;
  },
});

See examples/v7-advanced.md Pattern 6 for complete compute example.


Pattern 6: useFormContext for Nested Components

Use FormProvider + useFormContext to share form methods across deeply nested components without prop drilling. Ideal for multi-section forms and wizard steps.

// Parent
<FormProvider {...methods}>
  <form onSubmit={methods.handleSubmit(onSubmit)}>
    <AddressFields prefix="shippingAddress" />
    <AddressFields prefix="billingAddress" />
  </form>
</FormProvider>

// Child - no props needed
function AddressFields({ prefix }) {
  const { register } = useFormContext<CheckoutFormData>();
  return <input {...register(`${prefix}.street`)} />;
}

When to use: 3+ levels of nesting or reusable form sections. For 1-2 levels, passing control/register as props is simpler.

See examples/wizard.md for a complete multi-step wizard using FormProvider with per-step validation via trigger().


Pattern 7: Form Reset and Async Data

Two approaches for loading external data into a form:

  1. values prop (v7.x+, preferred): Reactively updates form when external data changes. Pair with resetOptions: { keepDirtyValues: true } to preserve user edits.

  2. reset() in useEffect (legacy): Manually reset when data arrives. Use reset(data) which updates both values AND defaultValues for proper isDirty tracking.

// Modern: values prop (reactive, auto-updates)
useForm<FormData>({
  values: userData,
  resetOptions: { keepDirtyValues: true },
});

// Legacy: manual reset
useEffect(() => {
  if (data) reset(data);
}, [data, reset]);

Cancel/save pattern: reset() without args reverts to defaultValues. After save, call reset(data) to update defaultValues and clear isDirty.

See examples/v7-advanced.md Pattern 1 for values prop with async data.


Pattern 8: Isolated Error Display

Use useFormState with name to create error components that only re-render when their specific field's error changes. For v7.68+, FormStateSubscribe provides the same isolation as a component.

function FieldError<T extends FieldValues>({ control, name }: Props<T>) {
  const { errors } = useFormState({ control, name });
  const error = errors[name];
  if (!error) return null;
  return <span role="alert">{error.message as string}</span>;
}

See examples/performance.md for a complete large form with isolated subscriptions, and examples/v7-advanced.md Pattern 4 for FormStateSubscribe.

</patterns>


Detailed Resources:


<red_flags>

RED FLAGS

High Priority Issues:

  • Using array index as key in useFieldArray -- causes state corruption on add/remove/reorder
  • Missing TypeScript generics on useForm -- loses type safety for field names and values
  • Using register for components that don't expose ref -- use Controller instead
  • Not providing defaultValues -- causes hydration mismatches and undefined warnings

Medium Priority Issues:

  • Using mode: "onChange" without reason -- validates on every keystroke, noisy UX
  • Destructuring many formState properties -- subscribes to all, causes unnecessary re-renders
  • Using watch() in render body -- triggers re-render on every field change; use useWatch instead
  • Calling setValue without shouldValidate: true -- may leave form in invalid state
  • Not using trigger(fieldNames) for step validation in wizard forms

Gotchas & Edge Cases:

  • reset() reverts to defaultValues; reset(newData) updates both values AND defaultValues
  • handleSubmit does not catch errors thrown in your onSubmit callback -- handle errors yourself with try/catch
  • append/prepend/insert require complete objects, not partial data
  • Array-level errors live at errors.arrayName.root, item errors at errors.arrayName[index].fieldName
  • shouldUnregister: true removes unmounted field values -- keep false (default) for wizard forms
  • useWatch returns defaultValue on first render before subscription kicks in
  • setValue does not directly update useFieldArray -- use replace() API instead
  • FormStateSubscribe works with control prop directly or via FormProvider (both are valid)
  • values prop (reactive external data) vs defaultValues (static initial values) -- do not mix their use cases

</red_flags>


<critical_reminders>

CRITICAL REMINDERS

All code must follow project conventions in CLAUDE.md

(You MUST provide generic types to useForm<FormData>() for type-safe form handling)

(You MUST use field.id as key prop in useFieldArray - NEVER use array index)

(You MUST use Controller for controlled components that don't expose a ref)

(You MUST use resolver pattern for schema validation - keep schemas separate from form logic)

(You MUST set mode: "onBlur" or mode: "onTouched" for optimal UX - avoid mode: "onChange" unless needed)

Failure to follow these rules will break form validation, cause re-render issues, and reduce type safety.

</critical_reminders>