--- title: Use useFormContext Sparingly for Deep Nesting impact: MEDIUM impactDescription: reduces prop drilling but increases implicit dependencies tags: sub, useFormContext, FormProvider, prop-drilling --- ## Use useFormContext Sparingly for Deep Nesting useFormContext eliminates prop drilling by accessing form methods via context, but creates implicit dependencies that are harder to track. Use it for deeply nested components; prefer explicit props for shallow nesting. **Incorrect (useFormContext for shallow nesting):** ```typescript function ContactForm() { const methods = useForm() return (
{/* One level deep, context overhead not needed */}
) } function NameInput() { const { register } = useFormContext() // Implicit dependency return } ``` **Correct (explicit props for shallow nesting):** ```typescript function ContactForm() { const { register, handleSubmit } = useForm() return (
{/* Explicit dependency */} ) } function NameInput({ register }: { register: UseFormRegister }) { return } ``` **When to use useFormContext:** - Components nested 3+ levels deep - Shared components used across multiple forms - Complex form sections with many fields Reference: [useFormContext](https://react-hook-form.com/docs/useformcontext)