useWatch does not update when a deeply nested field changes
Version Number
^7.89.0
Codesandbox/Expo snack
not exist
Steps to reproduce
GitHub IssueuseWatch does not update when a deeply nested field changes
Description
I am experiencing an issue with useWatch where subscriptions to parent paths do not update when a deeply nested field changes.
I am currently using:
"react-hook-form": "^7.89.0"
Example
My form contains a structure similar to:
steps
└── [index]
└── values
└── [AT_IMAGES_REFS]
└── values
└── [0]
└── value
The field is registered using Controller:
<Controller
name={`steps.${index}.values.${AT_IMAGES_REFS}.values[0].value`}
control={control}
render={({ field: { onChange, value } }) => (
<FormImageSelector
uri={imageUrl}
onPress={() => handleUploadImage(onChange)}
onPressRemove={() => handleRemoveImage(onChange, value)}
/>
)}
/>
Watching the parent path
When watching the complete steps array:
const steps = useWatch({
control,
name: "steps",
});
the value does not update when a nested field changes.
The same happens when watching a single step:
const step = useWatch({
control,
name: `steps.${index}`,
});
For example, changing:
steps.${index}.values.${AT_IMAGES_REFS}.values[0].value
does not trigger either watcher.
This also applies to other nested fields such as:
steps.${index}.values.${AT_TITLE}.value.value
or:
steps.${index}.values.${AT_DESCRIPTION}.value.value
Exact field subscription works
When subscribing directly to the changed field, everything works as expected:
const imageRef = useWatch({
control,
name: `steps.${index}.values.${AT_IMAGES_REFS}.values[0].value`,
});
imageRef receives the updated value correctly.
So the behavior is:
exact option
I also tested both values of exact:
useWatch({
control,
name: "steps",
exact: false,
});
and:
useWatch({
control,
name: "steps",
exact: true,
});
Neither resolves the issue.
The direct field subscription continues to work correctly.
Expected behavior
I would expect a subscription to a parent path such as:
useWatch({
control,
name: "steps",
});
to be notified when a descendant field changes, for example:
steps
└── 0
└── values
└── <AT_IMAGES_REFS>
└── values
└── 0
└── value ← changed
Likewise, subscribing to:
useWatch({
control,
name: "steps.0",
});
should receive the update.
Actual behavior
Only the subscription to the exact field path is updated.
steps → ❌
steps.0 → ❌
steps.0.values.<AT_IMAGES_REFS>.values[0].value → ✅
Question
Is this the expected behavior of useWatch in react-hook-form@^7.89.0?
If parent-path subscriptions are expected to receive updates from deeply nested fields, this appears to be a regression or subscription issue.
Expected behaviour
Hope it helps
What browsers are you seeing the problem on?
No response
Relevant log output
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: react-hook-form/react-hook-form