admin管理员组文章数量:1123589
I know react-hook-form does not set isDirty
state true byDefault, but for some unknown reasons, My form's isDirty
state is being set as true when the form is initialized.
Here's my code:
const {
register,
handleSubmit,
watch,
setValue,
setError,
control,
formState: { isSubmitting, errors, isDirty },
} = useForm<IEditProfileAboutForm>({
defaultValues: defaultValues,
resolver: zodResolver(aboutFormSchema),
})
const {
fields: socialMediaLinksFields,
append: socialMediaLinksAppend,
remove: socialMediaLinksRemove,
} = useFieldArray({
control,
name: 'social_media_links',
})
const {
fields: otherLinksFields,
append: otherLinksAppend,
remove: otherLinksRemove,
} = useFieldArray({
control,
name: 'other_links',
})
const {
fields: customFieldsFields,
append: customFieldsAppend,
remove: customFieldsRemove,
} = useFieldArray({
control,
name: 'custom_field',
})
Below is my defaultValues
object:
const defaultValues = {
about: userData?.about || '',
about_video: userData?.about_video || '',
about_image_file: userData?.about_image_file || null,
about_image: userData?.about_image || '',
about_image_url: userData?.about_image_url || '',
company_about: userData?pany?.about || '',
company_about_video: userData?pany?.about_video || '',
social_media_links:
(isIndividualUser
? userData?.social_media_links
: userData?pany?.social_media_links
)?.map((link: any, index: number) => {
return {
...link,
is_allow_to_personalize: link?.is_allow_to_personalize,
link: link?.is_allow_to_personalize
? userData?.social_media_links?.find(
(item: any) => item.id === link.id,
)?.link || link?.link
: link?.link,
url: link?.is_allow_to_personalize
? userData?.social_media_links?.find(
(item: any) => item.id === link.id,
)?.url ||
link?.url ||
''
: link?.url || '',
}
}) || [],
other_links:
(isIndividualUser
? userData?.other_links
: userData?pany?.other_links
)?.map((link: any, index: number) => {
return {
...link,
is_allow_to_personalize: link?.is_allow_to_personalize,
url: link?.is_allow_to_personalize
? userData?.other_links?.find(
(item: any) => item.id === link.id,
)?.url ||
link?.url ||
''
: link?.url || '',
field_name: link?.is_allow_to_personalize
? userData?.other_links?.find(
(item: any) => item.id === link.id,
)?.field_name ||
link?.field_name ||
''
: link?.field_name || '',
}
}) || [],
custom_field:
(isIndividualUser
? userData?.custom_field
: userData?pany?.custom_field
)?.map((link: any, index: number) => {
return {
...link,
is_allow_to_personalize: link?.is_allow_to_personalize,
field_value: link?.is_allow_to_personalize
? userData?.custom_field?.find(
(item: any) => item.id === link.id,
)?.field_value || link?.field_value
: link?.field_value,
}
}) || [],
}
After this if i console.log({isDirty})
, I'm getting below output:
now one might bethinking that i might be setting values in useEffect
using setValue
.
But below is the only useEffect i have in this component:
useEffect(() => {
if (Object.values(errors)?.length > 0) {
helper.ToastSchemaErrorMessages(errors)
setIsFormLoading(false)
}
}, [errors])
I've been trying to fix this issue for hrs, let me know if you guys find something fishy in my code!
Thanks in advance!
本文标签: reactjsisDirty state is set to true when form is initializedStack Overflow
版权声明:本文标题:reactjs - isDirty state is set to true when form is initialized - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736580198a1944939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论