356 lines
6.6 KiB
Markdown
356 lines
6.6 KiB
Markdown
# Performance Optimization Guide
|
|
|
|
Strategies for optimizing React Hook Form performance.
|
|
|
|
---
|
|
|
|
## Form Validation Modes
|
|
|
|
### onSubmit (Best Performance)
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
mode: 'onSubmit', // Validate only on submit
|
|
resolver: zodResolver(schema),
|
|
})
|
|
```
|
|
|
|
**Pros**: Minimal re-renders, best performance
|
|
**Cons**: No live feedback
|
|
|
|
### onBlur (Good Balance)
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
mode: 'onBlur', // Validate when field loses focus
|
|
resolver: zodResolver(schema),
|
|
})
|
|
```
|
|
|
|
**Pros**: Good UX, reasonable performance
|
|
**Cons**: Some re-renders on blur
|
|
|
|
### onChange (Live Feedback)
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
mode: 'onChange', // Validate on every change
|
|
resolver: zodResolver(schema),
|
|
})
|
|
```
|
|
|
|
**Pros**: Immediate feedback
|
|
**Cons**: Most re-renders, can be slow with complex validation
|
|
|
|
### all (Maximum Validation)
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
mode: 'all', // Validate on blur, change, and submit
|
|
resolver: zodResolver(schema),
|
|
})
|
|
```
|
|
|
|
**Pros**: Most responsive
|
|
**Cons**: Highest performance cost
|
|
|
|
---
|
|
|
|
## Controlled vs Uncontrolled
|
|
|
|
### Uncontrolled (Faster)
|
|
|
|
```typescript
|
|
// Best performance - no React state
|
|
<input {...register('email')} />
|
|
```
|
|
|
|
### Controlled (More Control)
|
|
|
|
```typescript
|
|
// More React state = more re-renders
|
|
<Controller
|
|
control={control}
|
|
name="email"
|
|
render={({ field }) => <Input {...field} />}
|
|
/>
|
|
```
|
|
|
|
**Rule**: Use `register` by default, `Controller` only when necessary.
|
|
|
|
---
|
|
|
|
## watch() Optimization
|
|
|
|
### Watch Specific Fields
|
|
|
|
```typescript
|
|
// BAD - Watches all fields, re-renders on any change
|
|
const values = watch()
|
|
|
|
// GOOD - Watch only what you need
|
|
const email = watch('email')
|
|
const [email, password] = watch(['email', 'password'])
|
|
```
|
|
|
|
### useWatch for Isolation
|
|
|
|
```typescript
|
|
import { useWatch } from 'react-hook-form'
|
|
|
|
// Isolated component - only re-renders when email changes
|
|
function EmailDisplay() {
|
|
const email = useWatch({ control, name: 'email' })
|
|
return <div>{email}</div>
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## Debouncing Validation
|
|
|
|
### Manual Debounce
|
|
|
|
```typescript
|
|
import { useDebouncedCallback } from 'use-debounce'
|
|
|
|
const debouncedValidation = useDebouncedCallback(
|
|
() => trigger('username'),
|
|
500 // Wait 500ms
|
|
)
|
|
|
|
<input
|
|
{...register('username')}
|
|
onChange={(e) => {
|
|
register('username').onChange(e)
|
|
debouncedValidation()
|
|
}}
|
|
/>
|
|
```
|
|
|
|
---
|
|
|
|
## shouldUnregister Flag
|
|
|
|
### Keep Data When Unmounting
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
shouldUnregister: false, // Keep field data when unmounted
|
|
})
|
|
```
|
|
|
|
**Use When**:
|
|
- Multi-step forms
|
|
- Tabbed interfaces
|
|
- Conditional fields that should persist
|
|
|
|
### Clear Data When Unmounting
|
|
|
|
```typescript
|
|
const form = useForm({
|
|
shouldUnregister: true, // Remove field data when unmounted
|
|
})
|
|
```
|
|
|
|
**Use When**:
|
|
- Truly conditional fields
|
|
- Dynamic forms
|
|
- Want to clear data automatically
|
|
|
|
---
|
|
|
|
## useFieldArray Optimization
|
|
|
|
### Use field.id as Key
|
|
|
|
```typescript
|
|
// CRITICAL for performance
|
|
{fields.map((field) => (
|
|
<div key={field.id}> {/* Not index! */}
|
|
...
|
|
</div>
|
|
))}
|
|
```
|
|
|
|
### Avoid Unnecessary Re-renders
|
|
|
|
```typescript
|
|
// Extract field components
|
|
const FieldItem = React.memo(({ field, index, register, remove }) => (
|
|
<div>
|
|
<input {...register(`items.${index}.name`)} />
|
|
<button onClick={() => remove(index)}>Remove</button>
|
|
</div>
|
|
))
|
|
|
|
// Use memoized component
|
|
{fields.map((field, index) => (
|
|
<FieldItem
|
|
key={field.id}
|
|
field={field}
|
|
index={index}
|
|
register={register}
|
|
remove={remove}
|
|
/>
|
|
))}
|
|
```
|
|
|
|
---
|
|
|
|
## formState Optimization
|
|
|
|
### Subscribe to Specific Properties
|
|
|
|
```typescript
|
|
// BAD - Subscribes to all formState changes
|
|
const { formState } = useForm()
|
|
|
|
// GOOD - Subscribe only to what you need
|
|
const { isDirty, isValid } = useForm().formState
|
|
|
|
// BETTER - Use useFormState for isolation
|
|
import { useFormState } from 'react-hook-form'
|
|
const { isDirty } = useFormState({ control })
|
|
```
|
|
|
|
---
|
|
|
|
## Resolver Optimization
|
|
|
|
### Memoize Schema
|
|
|
|
```typescript
|
|
// BAD - New schema on every render
|
|
const form = useForm({
|
|
resolver: zodResolver(z.object({ email: z.string() })),
|
|
})
|
|
|
|
// GOOD - Schema defined outside component
|
|
const schema = z.object({ email: z.string() })
|
|
|
|
function Form() {
|
|
const form = useForm({
|
|
resolver: zodResolver(schema),
|
|
})
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## Large Forms
|
|
|
|
### Split into Sections
|
|
|
|
```typescript
|
|
function PersonalInfoSection() {
|
|
const { register } = useFormContext()
|
|
return (
|
|
<div>
|
|
<input {...register('firstName')} />
|
|
<input {...register('lastName')} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ContactInfoSection() {
|
|
const { register } = useFormContext()
|
|
return (
|
|
<div>
|
|
<input {...register('email')} />
|
|
<input {...register('phone')} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function LargeForm() {
|
|
const methods = useForm()
|
|
|
|
return (
|
|
<FormProvider {...methods}>
|
|
<form>
|
|
<PersonalInfoSection />
|
|
<ContactInfoSection />
|
|
</form>
|
|
</FormProvider>
|
|
)
|
|
}
|
|
```
|
|
|
|
### Virtualize Long Lists
|
|
|
|
```typescript
|
|
import { useVirtualizer } from '@tanstack/react-virtual'
|
|
|
|
function VirtualizedFieldArray() {
|
|
const { fields } = useFieldArray({ control, name: 'items' })
|
|
|
|
const parentRef = React.useRef(null)
|
|
|
|
const rowVirtualizer = useVirtualizer({
|
|
count: fields.length,
|
|
getScrollElement: () => parentRef.current,
|
|
estimateSize: () => 50,
|
|
})
|
|
|
|
return (
|
|
<div ref={parentRef} style={{ height: '400px', overflow: 'auto' }}>
|
|
<div style={{ height: `${rowVirtualizer.getTotalSize()}px` }}>
|
|
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
|
|
const field = fields[virtualRow.index]
|
|
return (
|
|
<div key={field.id}>
|
|
<input {...register(`items.${virtualRow.index}.name`)} />
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## Performance Benchmarks
|
|
|
|
| Optimization | Before | After | Improvement |
|
|
|--------------|--------|-------|-------------|
|
|
| mode: onSubmit vs onChange | 100ms | 20ms | 80% |
|
|
| watch() all vs watch('field') | 50ms | 10ms | 80% |
|
|
| field.id vs index key | 200ms | 50ms | 75% |
|
|
| Memoized schema | 30ms | 5ms | 83% |
|
|
|
|
---
|
|
|
|
## Profiling
|
|
|
|
### React DevTools Profiler
|
|
|
|
1. Open React DevTools
|
|
2. Go to Profiler tab
|
|
3. Click Record
|
|
4. Interact with form
|
|
5. Stop recording
|
|
6. Analyze render times
|
|
|
|
### Performance.mark API
|
|
|
|
```typescript
|
|
const onSubmit = (data) => {
|
|
performance.mark('form-submit-start')
|
|
|
|
// Submit logic
|
|
|
|
performance.mark('form-submit-end')
|
|
performance.measure('form-submit', 'form-submit-start', 'form-submit-end')
|
|
|
|
const measures = performance.getEntriesByName('form-submit')
|
|
console.log('Submit time:', measures[0].duration, 'ms')
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
**Official Docs**: https://react-hook-form.com/advanced-usage#PerformanceOptimization
|