Next.js Write (Reuse-First, Maintainable, RHF + Zod)
Use this skill when implementing or refactoring Next.js (TypeScript/React) code in a TypeScript/React codebase.
Core Requirements (Must Follow)
- Use
next/image(<Image />) for rendering images. - Reuse existing code before creating anything new:
- Functional components (FCs)
- Hooks
- Utilities and constants
- UI primitives
- Prioritize
global/,shared/,components/,ui/,hooks/,lib/,utils/
- Keep FCs small and single-responsibility:
- Split large components into
XxxSection,XxxForm,XxxField,XxxList,XxxItem. - Keep helper functions near usage.
- Split large components into
- Build forms with React Hook Form + Zod by default:
- Use
zodResolverfrom@hookform/resolvers/zod.
- Use
Autonomous Development Workflow
- Do not read or edit files outside the project folder.
- Work autonomously in small, testable increments.
- Understand existing routing, data fetching, and UI patterns before implementing.
- Do not commit changes.
- After implementation, run project checks in expected order. If available, use
typescript-postcheck.
Reuse-First Search Strategy (Required)
Before creating new code:
- Find an existing component matching UX/layout.
- Find an existing hook matching state/data/side-effect needs.
- Find existing utilities for parsing, formatting, API calls, and error handling.
- Create the smallest possible new code only when reuse is not viable.
When reuse is not possible, state the reason clearly:
- API mismatch
- Missing variant
- Coupling constraints
- Performance constraints
Next.js Conventions
Routing and Rendering
- Prefer Server Components for data fetching and composition when using App Router.
- Use Client Components only when needed (state, effects, handlers, RHF).
- Keep server/client boundaries explicit (
"use client"only where required).
Data Fetching
- Reuse existing fetch wrappers from
lib/api,services, orclientwhen present. - Avoid ad-hoc fetch logic in multiple components.
- Handle loading and error states consistently with existing patterns.
Images
- Always:
import Image from "next/image";- Provide
alt - Provide
width/heightor usefillwith a sized parent - Provide
sizesfor responsive layouts when applicable
React Component Style (Maintainable FCs)
- Keep components small and focused.
- Use
Propsinterfaces. - Use early returns for empty/error states.
- Avoid deeply nested JSX; extract subcomponents.
- Colocate state with the smallest responsible owner.
Forms (React Hook Form + Zod First)
When building forms:
- Define or reuse a Zod schema near the form.
- Infer types with
type FormValues = z.infer<typeof schema>. - Use
useForm<FormValues>({ resolver: zodResolver(schema), defaultValues }). - Use
FormProviderand small field components when readability improves. - Use
Controlleronly for non-native/custom inputs.
Required Zod practices:
- Reuse schemas from
global/orshared/when available. - Keep schemas narrow and aligned with real constraints.
- Prefer
.trim(),.min(),.max(),.email(), and explicit messages. - Validate at boundaries (for example API payloads), not only in UI.
Typical structure:
XxxFormowns schema,useForm, submitXxxFieldsgroups field layoutsXxxFieldencapsulates single reusable field
Anti-Patterns to Avoid
- Avoid
<img>instead ofnext/image. - Avoid duplicating existing shared/global components/hooks/schemas.
- Avoid large multi-responsibility "God components."
- Avoid speculative abstractions with one caller.
- Avoid hand-rolled validation when RHF + Zod is available.
Suggested Workflow (Checklist)
- Identify the closest existing page/component pattern.
- Search shared/global directories for reusable FCs/hooks/utils/schemas.
- Implement the smallest change using small components.
- Use RHF + Zod for forms by default.
- Ensure all images use
next/image. - Run targeted checks/tests, then lint/typecheck per repo convention.
- Keep diffs focused and reviewable.
Output Expectations
- Mention which components/hooks/utils/schemas were reused and from where.
- Keep new components small and clearly named.
- Use
next/imagefor all image rendering. - Use React Hook Form + Zod validation by default for forms.