can not reset undefined to a value #5858
Replies: 7 comments 7 replies
-
we don't support input to be |
Beta Was this translation helpful? Give feedback.
-
Component should not switch between controlled or uncontrolled behaviour. And |
Beta Was this translation helpful? Give feedback.
-
This seems super broken to me. I have the following scenario: const schema = z.object({
tournament_pairing_id: z.string().uuid(),
player_0_id: z.string().uuid(),
player_1_id: z.string().uuid(),
});
export type FormData = z.infer<typeof schema>;
const form = useForm<FormData>({
resolver: zodResolver(schema),
defaultValues: {
tournament_pairing_id: undefined, // Should be null
player_0_id: undefined, // Should be null
player_1_id: undefined, // Should be null
},
mode: 'onSubmit',
});
// Reset player values if pairing is changed
useEffect(() => {
reset((prev) => ({ ...prev, player_0_id: null, player_1_id: null }));
}, [reset, tournament_pairing_id]); Trying to reset to Ideally I'd actually set the default values to IMO: defaultValues and reset should not be checked against the form's type. That type represents valid form data than be submitted, not the default values. I can sort of work around with using empty strings to mean |
Beta Was this translation helpful? Give feedback.
-
So... there is no way of setting the field value back to undefined ? Not even |
Beta Was this translation helpful? Give feedback.
-
I think it is weird that there is only one type for both before and after submit. It feels natural and intuitive to have empty fields in web forms, especially in default states and dependent fields where changing one clears the other. I agree with @ianpaschal about nullable types, all methods other than handleSubmit accepting |
Beta Was this translation helpful? Give feedback.
-
I ran into similar problem.
|
Beta Was this translation helpful? Give feedback.
-
For whoever is facing this issue, use this wrapper when using form.reset(replaceUndefinedWithNull(yourFormValuesWithUndefinedValues))` form.reset(replaceUndefinedWithNull(yourFormValuesWithUndefinedValues))`
/**
* We need this to use with react hook form.
* Setting objects to undefined on form.reset will be ignored and the field won't be updated.
* This function will replace undefined with null
*/
export function replaceUndefinedWithNull<T>(obj: T): T {
if (Array.isArray(obj)) {
return obj.map(replaceUndefinedWithNull) as unknown as T
} else if (obj !== null && typeof obj === "object") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const newObj: any = {}
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) {
newObj[key] = null
} else {
newObj[key] = replaceUndefinedWithNull(value)
}
}
return newObj
}
return obj
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
reset({ position: undefined });
like above, this position value can not be reset into undefined。
I think it is inconvenient if react-use-form transform undefined unconsciously in both reset or defaultValues.
I really hope this can be fixed.
Beta Was this translation helpful? Give feedback.
All reactions