-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
track issue here: #7652 |
Beta Was this translation helpful? Give feedback.
-
Looks like an issue as the const { reset, getValues } = useForm()
reset({ ...getValues(), xmlFile: null }) Take a look at the Props section of RHF docs on |
Beta Was this translation helpful? Give feedback.
-
I am having a similar issue with a controlled "field" that receives a value of type In my case, we have a multi-step form. The parent component holds the form state, and it passes it down to each step child components through props, which are then passed to the Using Here's an example: export type UserInformationStepForm = {
firstName: string;
lastName: string;
timeZone: string;
avatar?: File | null;
};
export function useHandleForm({
selectedAvatar,
selectedFirstName,
selectedLastName,
selectedTimeZone,
onGoToNextStep,
}: HandleFormProps) {
const formSchema: Yup.Schema<UserInformationStepForm> = Yup.object().shape({
avatar: Yup.mixed<File>().nullable(),
firstName: Yup.string().required(),
lastName: Yup.string().required(),
timeZone: Yup.string().required(),
});
const defaultValues = useMemo(
() => ({
avatar: selectedAvatar ?? null, // This one is needed due to react-hook-form... since undefined does not work
firstName: selectedFirstName,
lastName: selectedLastName,
timeZone: selectedTimeZone,
}),
[selectedAvatar, selectedFirstName, selectedLastName, selectedTimeZone],
);
const {
control,
register,
handleSubmit,
formState: { errors, isSubmitting, isValid },
} = useForm<UserInformationStepForm>({
mode: 'all',
resolver: yupResolver(formSchema),
defaultValues,
});
useEffect(() => {
reset(defaultValues);
}, [reset, defaultValues]);
const onSubmit = handleSubmit(() => {
onGoToNextStep?.();
});
return {
control,
errors,
register,
disabledSubmit: isSubmitting || !isValid || !onGoToNextStep,
onSubmit,
};
} And this is our controlled <Controller
control={control}
name="avatar"
render={({ field: { value, onChange } }) => {
return (
<ProfilePictureUploader
image={value}
onChangeImageFile={(newValue) => {
onChange(newValue);
// onChangeForm updates the form state from outside, and the component gets re-rendered with the newly updated props
// so these props are then passed to my useHandleForm custom hook's memoized defaultValues object,
// triggering the useEffect, and updating the form's defaultValues
onChangeForm({ avatar: newValue });
}}
/>
);
}}
/> Any ideas as to how we could get rid of this Also, sending an empty string would force us to modify Yup's schema and our types as well, which is still not desired. It seems like React Hook Form is not updating the default value of a field when its initial value is of type |
Beta Was this translation helpful? Give feedback.
-
location.reload() works fine to me :) |
Beta Was this translation helpful? Give feedback.
Looks like an issue as the
reset
form method works as expected. See #7652.Meanwhile you can try using the [the
reset
form method](https://react-hook-form.com/api/useform/reset methodTake a look at the Props section of RHF docs on
reset
to see what additional options you want to provide to keep you form state