Replies: 11 comments 1 reply
-
please share a codesandbox and steps to reproduce: #2848 |
Beta Was this translation helpful? Give feedback.
-
It took me some time to find the issue. The problem appear when I use setValue directly to change a field value, just like this I also prepared an Expo Snack , but I think is not really necessary. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the Expo Snack. It's common sense to provide a reproduction when you're asking for support. There was no To the question at hand, I suspect what you're experiencing is the expected behavior. |
Beta Was this translation helpful? Give feedback.
-
@Moshyfawn this is the idea, intentionally I did not set shouldDirty to true, probably in this case isDirty should remain also false? Otherwise isDirty and dirtyFields are not in sync |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 @Moshyfawn sorry for not being clearly from the start, it was difficult for me to figure the real issue. Now I am ready to share the issue with you and steps to reproduce. The Expo Snack has been updated.
can you see the issue now? in step 1 isDirty is false and in step 3 is true and will remain true from now on. Note1. It does not make sense to have I also noticed that Note2. when |
Beta Was this translation helpful? Give feedback.
-
Pls ignore the above mentions, wrong link |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 I have some questions
In my opinion it's wrong by design to end having a dirty form without knowing what was actually changed. |
Beta Was this translation helpful? Give feedback.
-
This discrepancy is going to be the primary reason I have to replace this library with something else. This is really just the straw that broke the camel's back, but boy is it a heavy straw. Here are some other annoyances:
|
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I've created a custom dev tool for react hook form that discovers scenarios such as why isDirty is true and dirtyFields is empty. I'll share it if I receive any requests... (Actually, I created it because I suffered from this unclear behavior) @bluebill1049 if you see this feature useful in the official dev tool, let me know. |
Beta Was this translation helpful? Give feedback.
-
Here's my workaround - I created an extended version of useForm hook, which I call useVHook (but you can call it whatever you like. It has another additional check called isProperDirty which is basically going to check for number of dirty fields inside import {
FieldValues,
FormState,
UseFormProps,
UseFormReturn,
useForm,
} from 'react-hook-form';
interface ExtendedFormState<TFieldValues extends FieldValues>
extends FormState<TFieldValues> {
isProperDirty: boolean;
}
interface UseFormReturnExtended<TFieldValues, TContext, TTransformedValues>
extends UseFormReturn {
formState: ExtendedFormState<TFieldValues>;
}
const useVForm = <
TFieldValues extends FieldValues = FieldValues,
TContext = any,
TTransformedValues extends FieldValues | undefined = undefined,
>(
props?: UseFormProps<TFieldValues, TContext>,
): UseFormReturnExtended<TFieldValues, TContext, TTransformedValues> => {
const formMethods = useForm(props);
return {
...formMethods,
formState: {
...formMethods.formState,
isProperDirty: Object.keys(formMethods.formState.dirtyFields).length > 0,
},
} as UseFormReturnExtended<TFieldValues, TContext, TTransformedValues>;
};
export default useVForm; |
Beta Was this translation helpful? Give feedback.
-
Version Number
7.27.0
Codesandbox/Expo snack
not provided
Steps to reproduce
I am not sure if it is an issue or this should be the expected behaviour, but if I:
isDirty is true but dirtyFields is an empty object
for me seems to be confusing
Expected behaviour
isDirty false
dirtyFields empty object
What browsers are you seeing the problem on?
No response
Relevant log output
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions