Shipnative uses React Hook Form for form state and Zod for validation. This combo gives you type-safe, performant forms with minimal code.
For vibecoders: Just tell your AI “create a form with email and password fields” and it’ll generate this pattern.
React Hook Form is a popular library that helps you build forms with less code and better performance. It focuses on uncontrolled components, reducing re-renders and improving overall responsiveness.
Key Features
- Performance: Minimizes re-renders by isolating component updates.
- Developer Experience: Simple API with intuitive hooks.
- Validation Integration: Seamlessly integrates with schema validation libraries like Zod.
- Uncontrolled Components: Leverages native HTML form validation and reduces component re-renders.
Basic Usage
- Import
useForm:
- Initialize
useForm:
Zod: Type-Safe Schema Validation
Zod is a TypeScript-first schema declaration and validation library. It allows you to define your data shapes with a concise API, and then validate data against those schemas, providing excellent type inference and robust error reporting.
Key Features
- TypeScript-first: Provides powerful type inference, ensuring your forms are type-safe from definition to submission.
- Concise API: Easy to define complex validation schemas.
- Runtime Validation: Validates data at runtime, catching errors early.
- Customizable Errors: Full control over error messages.
Basic Usage
- Define Your Schema:
- Integrate with React Hook Form: Use the
resolver option with @hookform/resolvers/zod.
Best Practices
- Centralize Schemas: Define your Zod schemas in a dedicated
schemas/ directory or alongside your form components for easy access and reusability.
- Custom Components: Create reusable form input components (like
TextField above) that integrate seamlessly with Controller from React Hook Form. This abstracts away the field props and provides a cleaner API.
- Error Display: Clearly display validation errors to the user using the
formState.errors object.
- Type Inference: Always infer your form data types directly from your Zod schemas (
z.infer<typeof yourSchema>) to ensure maximum type safety.
Resources