Replies: 2 comments
|
I reproduced this with // getValues('movie') is unchanged
dirtyFields.movie = [{ id: false, title: false }];
!!dirtyFields.movie; // true: the array itself is truthyField-array operations can recalculate the form's dirty-field structure, including unrelated fields. This does not necessarily mean their values changed. The 7.60.0 implementation compares the leaves against For your separate movie-dirty flag, check for an actual function hasDirtyLeaf(value: unknown): boolean {
if (value === true) return true;
if (Array.isArray(value)) return value.some(hasDirtyLeaf);
if (value && typeof value === 'object') {
return Object.values(value).some(hasDirtyLeaf);
}
return false;
}
// Read dirtyFields during render so RHF subscribes to its updates.
const { dirtyFields } = formState;
const isMovieDirty = hasDirtyLeaf(dirtyFields.movie);This handles both a direct boolean and a nested object/array, including a tree whose leaves are all Also provide the initial In my minimal reproduction, 7.90.0 omits the unchanged AI-assisted verification: tested append, remove, editing |
|
The important distinction here is between tracking whether the entire When If the input is something like const { getValues, formState: { defaultValues } } = useFormContext();
const titleChanged =
getValues("movie.title") !== defaultValues?.movie?.title;For reactive updates, consider const title = useWatch({
control,
name: "movie.title",
});
const titleChanged = title !== initialTitle;Alternatively, if your unrelated input is currently nested under the same One thing I'd check in your CodeSandbox is whether the issue is simply the parent If the leaf field is changing unexpectedly, that would be worth reproducing against the latest React Hook Form version. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I have a form where I'm using
useFieldArray. This form also contain an unrelated input that I have to detect separately if it is dirty or not.!!dirtyFields.movieis working fine until I click onappend(or any of the other buttons) becausedirtyFields.movieis now an array with all of its properties. I don't get whyuseFieldArrayis messing with this field.https://codesandbox.io/p/sandbox/react-hook-form-usefieldarray-forked-hysfn9
I have found an issue about it (#11402), but it doesn't mention what should be done to properly handle that.
All reactions