--- title: Use useFormState for Isolated State Subscriptions impact: MEDIUM impactDescription: prevents parent re-renders from state access in children tags: formstate, useFormState, isolation, re-renders --- ## Use useFormState for Isolated State Subscriptions useFormState allows subscribing to form state in child components without causing parent re-renders. Each useFormState instance is isolated and doesn't affect other subscribers. **Incorrect (formState at root re-renders entire form):** ```typescript function ContactForm() { const { register, handleSubmit, formState: { errors, isDirty } } = useForm() return (
{errors.email && Email required} {/* Re-renders all on any state change */} {/* Prop drilling */} ) } ``` **Correct (useFormState isolates subscriptions):** ```typescript function ContactForm() { const { register, handleSubmit, control } = useForm() return (
{/* Isolated subscription */} ) } function EmailField({ register, control }: EmailFieldProps) { const { errors } = useFormState({ control, name: 'email' }) return (
{errors.email && Email required}
) } function SaveIndicator({ control }: { control: Control }) { const { isDirty } = useFormState({ control }) return isDirty ? Unsaved changes : null } ``` **Scope it further with `name`.** An isolated `useFormState({ control })` still re-renders on *any* field's state change. Pass `name` to narrow it to the fields the component actually displays: ```typescript function EmailFieldError({ control }: { control: Control }) { const { errors } = useFormState({ control, name: 'email' }) return errors.email ? {errors.email.message} : null } ``` Reference: [useFormState](https://react-hook-form.com/docs/useformstate)