react-hook-form-with-zod
React Hook Form With Zod
Use this skill for React Hook Form authoring and review. Start with sound RHF structure, then bring in Zod when the user needs schema validation, typed parsing, shared client/server validation, or modernization of stale Zod-backed forms.
Use This Skill When
- Building or reviewing React Hook Form forms
- Choosing between
register,useController,Controller, anduseFieldArray - Setting
defaultValues,formState,useFormState,setValue,reset,trigger,setError, or submit/error flows - Structuring deeply nested forms with
FormProvideranduseFormContext - Reviewing
watchversususeWatchsubscription choices - Handling numeric, date, checkbox, or custom widget input values in RHF
- Improving accessibility and error rendering for forms
- Adding Zod validation to an RHF form when schemas or shared validation are needed
- Debugging nested errors, field arrays, controlled inputs, or server validation roundtrips
- Reviewing or modernizing outdated React Hook Form + Zod examples
Out Of Scope
- Pure Zod schema design questions where React Hook Form is not the main problem
- General React component architecture that is unrelated to forms
- Framework-specific UI libraries beyond the form integration point
If the request is mostly about schema modeling or Zod migrations with little RHF involvement, keep the RHF advice brief and point the user toward zod-best-practices.
Working Style
- Identify whether the problem is plain RHF authoring, RHF review, or RHF plus schema validation.
- Prefer
registerfor native inputs,useControllerfor reusable controlled-field components, andControllerfor inline controlled bridges. - Solve browser-input normalization at the RHF boundary before adding schema transforms.
- Show the
useFormsetup, includingdefaultValues, validation strategy, subscriptions, and only the generics that materially help. - Introduce Zod when shared validation, parsed output typing, or clearer schema ownership actually improves the result.
- Call out stale or legacy RHF or Zod patterns when the user provides older examples.
Reference Map
references/resolver-setup-and-typing.md- Use for baseline
useFormsetup, RHF validation options,zodResolver, schema-derived typing, and transformed input/output types.
- Use for baseline
references/state-and-subscriptions.md- Use for
watch,useWatch,useFormState,getFieldState, and subscription-scoping guidance.
- Use for
references/form-input-normalization.md- Use for
valueAsNumber,valueAsDate,setValueAs, RHF-side normalization, optional schema coercion, and browser-input pitfalls.
- Use for
references/controlled-and-dynamic-fields.md- Use for
register,useController,Controller,useFieldArray, nested paths, controlled components, and dynamic form structures with or without Zod.
- Use for
references/composition-and-form-context.md- Use for
FormProvider,useFormContext, prop-passing tradeoffs, and nested form composition.
- Use for
references/error-flow-and-server-roundtrips.md- Use for RHF-only validation and mutation APIs,
setError,clearErrors,reset,trigger,setValue,rooterrors, and optional schema-backed server reuse.
- Use for RHF-only validation and mutation APIs,
references/performance-accessibility-and-troubleshooting.md- Use for validation modes, rerender control, accessibility, common RHF mistakes, and Zod-specific modernization notes.
Response Requirements
- Prefer RHF-first examples by default.
- Add Zod only when the user needs schema validation, shared validation, output typing, or a modernization path for existing Zod-backed code.
- Prefer current Zod 4 guidance whenever Zod is present.
- Cover the important RHF API that materially affects structure or performance instead of pretending
useFormplusregisteris the whole story. - If the user supplied stale code, separate concrete issues from optional improvements.
- Avoid turning the answer into a generic React tutorial when the problem is form wiring.
- Make the tradeoff between schema validation and RHF field-level rules explicit when both are present.
Core Defaults
- Prefer plain
useForm(...)with sensibledefaultValuesand RHF-native patterns for simple forms. - Prefer
resolver: zodResolver(schema)when schema-backed validation or shared client/server parsing is actually useful. - Prefer
type FormValues = z.infer<typeof schema>for standard Zod-backed forms, and reach forz.input<typeof schema>/z.output<typeof schema>when transforms or coercion change the parsed shape. - Prefer
register("age", { valueAsNumber: true }),valueAsDate, orsetValueAsfor native browser inputs before defaulting to schema coercion. - Prefer
registerfor native inputs,useControllerfor reusable controlled components, andControllerfor inline one-off controlled adapters. - Provide complete
defaultValuesatuseFormwhen RHF needs a stable source of truth; do not useundefinedas the controlled default value. - Prefer
useWatchover broadwatch()subscriptions when rerender isolation matters. - Prefer
useFormStateorgetFieldStatefor scoped state reads instead of pulling the entireformStateinto large components. - Prefer
FormProvideranduseFormContextfor deeply nested form trees, but avoid context when direct props keep dependencies clearer. - Prefer RHF rules for small local constraints and Zod when validation should be centralized, reused, or typed.
- Avoid duplicating the same validation in both the schema and
register(...)unless the duplication is intentional. - Prefer targeted
trigger("fieldName")and deliberatereset(...)/setValue(...)usage over broad form-wide updates when performance or state correctness matters. - Prefer
z.flattenError()when mapping form-field errors andz.treeifyError()when the UI or logs need nested structure. - Prefer
setError("root.serverError", ...)or field-specificsetError(...)for async server feedback after submission. - Prefer
field.idas the React key foruseFieldArrayitems. - Prefer array helpers such as
append,remove, andreplace; avoid whole-array updates viasetValue(...)when RHF documents that path as legacy or scheduled for removal.
Provenance
This skill is authored from the React Hook Form 7.71.2 documentation snapshot, with the bundled Zod 4.3.6 documentation.
More from mkgp-dev/codex-skills
tanstack-query-best-practices
Best-practice guidance for designing, reviewing, debugging, and modernizing TanStack Query v5 usage across React, Preact, Solid, Vue, Svelte, and Angular. Use this skill whenever the user is working with TanStack Query or React Query, `QueryClient` setup, `useQuery`, `useMutation`, `useInfiniteQuery`, query keys, invalidation, optimistic updates, prefetching, hydration, cache tuning, stale UI, or server-state boundaries and needs current guidance instead of stale v4-era patterns.
8zustand-state-management
Best-practice guidance for designing, reviewing, and modernizing Zustand state in modern TypeScript React apps. Use this skill whenever the user is writing or reviewing `create`, `createStore`, `useStore`, `useShallow`, `persist`, `devtools`, `subscribeWithSelector`, `createWithEqualityFn`, slices, Next.js store setup, hydration behavior, or selector-related rerender problems in Zustand v5 code.
7zod-best-practices
Core Zod schema design, validation, migration, and review guidance for TypeScript applications. Use this skill whenever the user is writing Zod schemas, reviewing Zod code, modernizing deprecated Zod patterns, choosing parsing or error-handling APIs, reasoning about type inference, composing advanced schemas, or asking for current best practices that should avoid stale Zod guidance.
7tanstack-router-best-practices
Best-practice guidance for designing, reviewing, debugging, and modernizing TanStack Router routing in React apps, especially file-based route trees, `Link` and `useNavigate`, `validateSearch`, `beforeLoad`, loaders, `notFoundComponent`, auth guards, code splitting, and TanStack Query integration. Use this whenever the user is working with `@tanstack/react-router`, TanStack Start routing, route organization, URL state, or navigation behavior and needs current v1 guidance instead of stale class-based or React Router patterns.
7changelog-maintenance
Best-practice guidance for creating, reviewing, and maintaining high-signal `CHANGELOG.md` entries for versioned software projects. Use this skill whenever the user wants to add or rewrite changelog entries, organize release history, turn completed work into structured version notes, decide what belongs in `Added`/`Changed`/`Fixed`/`Deprecated`/`Removed`/`Security`, or improve noisy release notes into maintainable repository changelogs.
5shadcn-ui-best-practices
Best-practice guidance for authoring, reviewing, debugging, and modernizing shadcn/ui usage in React applications. Use this skill whenever the user is working with shadcn/ui components, `components.json`, Dialog or Sheet flows, Field or FieldGroup forms, semantic tokens, theming, Tailwind v4 integration, or wants to replace ad hoc Tailwind-heavy UI with maintainable component-first patterns.
2