Build forms with React Hook Form and Zod validation
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: Performant Form Management
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:
import { useForm, Controller } from 'react-hook-form'import { TextField } from '@/../components/TextField' // Assuming a custom TextField componentimport { Button, View } from 'react-native'
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:
import { z } from 'zod'export const loginSchema = z.object({ email: z.string().email("Invalid email address"), password: z.string().min(6, "Password must be at least 6 characters long"),})// Infer the TypeScript type from the schemaexport type LoginFormData = z.infer<typeof loginSchema>
Integrate with React Hook Form: Use the resolver option with @hookform/resolvers/zod.
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.