Hooks
useForm
A versatile hook for managing form state, validation, and submission with a clean API.
useForm
The useForm
hook provides a comprehensive solution for handling form state, validation, error handling, and submission in React applications. It’s designed to reduce boilerplate while maintaining flexibility.
Login Form
Installation
Install the useForm hook using:
File Structure
use-form.ts
API
Options
Prop | Type | Default |
---|---|---|
initialValues | T (generic object type with field values) | Required |
onSubmit | (values: T) => Promise<void> | void | Required |
validate | (values: T) => Partial<Record<keyof T, string>> | undefined |
Form State
Prop | Type | Default |
---|---|---|
values | T (generic object type) | - |
errors | Partial<Record<keyof T, string>> | - |
touched | Partial<Record<keyof T, boolean>> | - |
isSubmitting | boolean | - |
isValid | boolean | - |
Form Actions
Prop | Type | Default |
---|---|---|
setFieldValue | (field: keyof T, value: any) => void | - |
setFieldError | (field: keyof T, error: string) => void | - |
setFieldTouched | (field: keyof T, isTouched?: boolean) => void | - |
setValues | (values: Partial<T>) => void | - |
setErrors | (errors: Partial<Record<keyof T, string>>) => void | - |
setTouched | (touched: Partial<Record<keyof T, boolean>>) => void | - |
resetForm | () => void | - |
validateForm | () => boolean | - |
submitForm | () => Promise<void> | - |
Examples
Contact Form with Validation
A complete contact form with validation and submission handling.
Contact Us
Form with External Validation Library (Zod)
You can integrate the useForm hook with external validation libraries like Zod.
Use Cases
- Login/Registration Forms: Manage authentication form state with validation
- Multi-step Forms: Break complex forms into manageable steps
- Data Collection Forms: Contact forms, surveys, and questionnaires
- Search & Filter Forms: Manage search parameters and filters
- CRUD Operations: Create and edit forms for database records
- Form Wizards: Guide users through complex form processes
- Dynamic Forms: Forms with fields that change based on user input
Accessibility
For building accessible forms, consider these practices alongside the useForm hook:
- Use proper HTML form elements (
<form>
,<label>
,<input>
, etc.) - Connect labels and inputs with matching
for
andid
attributes - Provide clear error messages that are announced to screen readers
- Use
aria-invalid
to indicate invalid fields - Ensure keyboard navigation works correctly
Best Practices
- Separate form logic from UI components for better maintainability
- Validate fields on both change and blur for a good UX balance
- Show validation errors only after fields have been touched
- Disable the submit button during submission to prevent double-submits
- Provide clear feedback during and after form submission
- Reset the form to its initial state after successful submission
- Use typed generics for type safety when working with TypeScript