Skip to content

Commit 2ab1b24

Browse files
docs: recommend useFormState for nested components with React Compiler (#1233)
useForm returns a stable object, so a compiled parent memoizes <FormProvider {...methods}> and useFormContext().formState stays stale. The migration guide now recommends useFormState({ control }) in nested components and destructured methods such as getFieldState.
1 parent 3ac1fe0 commit 2ab1b24

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

‎src/content/migrate-v7-to-v8.mdx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,24 @@ npm install react-hook-form@beta
1212

1313
## React Compiler {#ReactCompiler}
1414

15-
V8 adds first-class support for the [React Compiler](https://react.dev/learn/react-compiler). No additional configuration is required — React Hook Form is now compatible out of the box.
15+
V8 supports the [React Compiler](https://react.dev/learn/react-compiler). `useForm` returns the same object on every render, so the compiler can't tell when something read through that object changes. Two patterns keep compiled components up to date:
16+
17+
- In nested components, subscribe with [`useFormState`](/docs/useformstate) instead of reading `formState` from `useFormContext()`. The compiler memoizes `<FormProvider {...methods}>` in the parent, so the context keeps the `formState` from the first render.
18+
- Destructure methods such as `getFieldState` from `useForm()` instead of calling them on the returned object. The compiler memoizes `methods.getFieldState("name")` on `methods`, which never changes.
19+
20+
```tsx copy
21+
function NestedInput() {
22+
const { register, control } = useFormContext()
23+
const { errors } = useFormState({ control }) // ✅ updates under the compiler
24+
25+
return (
26+
<>
27+
<input {...register("name", { required: "Name is required" })} />
28+
<p>{errors.name?.message}</p>
29+
</>
30+
)
31+
}
32+
```
1633

1734
## Flat Field Array {#FlatFieldArray}
1835

0 commit comments

Comments
 (0)