ui: Unable to reset the Select component with React Hook Form
Calling form.reset() (React Hook Form reset method) does not reset the value selected by the user in the Select.
Here’s my useForm hook usage:
const form = useForm<z.infer<typeof someSchema>>({
resolver: zodResolver(someSchema),
})
Here’s my submit handler:
async function onSubmit(values: z.infer<typeof someSchema>) {
form.reset()
}
Here’s my Select field:
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
>
<FormField
control={form.control}
name="type"
render={({ field }) => (
<FormItem>
<FormLabel>Type</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select something" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="A">
A
</SelectItem>
<SelectItem value="B">
B
</SelectItem>
</SelectContent>
</Select>
</FormItem>
)}
/>
<Button type="submit">
Submit
</Button>
</form>
</Form>
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 14
- Comments: 48
Commits related to this issue
- chore: bump vitest to 1.6.0 (#549) * chore: bump vitest to 1.6.0 * fix: update — committed to u007/ui by Dunqing 3 years ago
Try using
valueinstead ofdefaultValueAs a temporary workaround, I have modified the
SelectTriggerto explicitly check thefield.value. If it is not set, it displays your placeholder.You also need to add the
valueprop to theSelectcomponent, as @riyaadh-abrahams suggested, in order to clear the selection when callingform.reset().i think you need to pass defaulValues
form.reset(defaultValues)I found the problem caused by the
onValueChangeevent inside the form. Try to modifyonChangeValueand usevalueinstead ofdefaultValueI have tried this and setting the
defaultValuestoundefined. Whenform.reset()is called, the<Select value={field.value}>doesn’t reset back to the original where the<SelectValue placeholder='Select a role'>.Accidentally found a better way to reset the Select:
The values were actually resetting but the select component wasn’t aware of the updated values. So we can force the reset by adding a key that changes as the value of the field changes. Assuming each of your select field has unique value, we can set
key={field.value}to trigger it with react-hook-form 'sreset()orresetField("YOUR_FIELD_NAME")functionsHey guys, there is a bug in Radix if you want to reset the value and show the placeholder again. It was fixed in https://github.com/radix-ui/primitives/pull/2174 and you can reset to placeholder using
"". For now, it’s only available on RC https://www.npmjs.com/package/@radix-ui/react-select?activeTab=versionsHi,
The
Selectcomponent was not resetting correctly becauseSelectPrimitive.Rootmaintains its own internal state. To fix this, we need to introduced an internal state (internalValue) that syncs with thevalueprop usinguseEffect, ensuring proper resets.Updated Select Component
Example Usage
We can use
{...field}oronValueChange={field.onChange} value={field.value}.Now, Select properly resets when using react-hook-form.
This is also the only solution I’ve found to work, every other suggestion here came with unwanted side-effects.
For me this is working fine on July 2024
This is the only solution that works for me so far. It appears that when the form initially loads, the
field.valueis correct, but then aonValueChangeevent is fired with an argument of"", wiping out the correct value. I haven’t found what triggers this errantonValueChangecall but it appears to be the cause of the issue.Hi @asafeclemente , you can change like that. change : defaultValue={field.value} -> value={field.value}
I tried doing this and I does reset the values however it doesn’t the entered value on the input field resulting in something like this
As @joaom00 said, you need to upgrade to @radix-ui/react-select@2.0.0-rc.9. I got the desired behavior by setting the default value to
""and addingvalue={field.value}to the Select.This solution helped me resetting my Select component. Thanks for this!
Hello! I tried it and it works. But now I have an error in the console browser:
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? Check the render method of 'Controller'.react hook form reset has bug !
use
form.reset, form can’t rerender select. so u can see it not working. butform.setValuecan working and rerender.and how to use it for 100% working
just like this
reset single form item
reset all form item
same problem
This work for me, and don’t setting
defaultValuestoundefined, otherwise, this will not work.This worked, thank you very much, @RinNguyens !
Is there a similar problem with Radio Group? I’m trying to use it in the exact way indicated:
And I’m having the problem that form.reset doesn’t reset this field
Hi! I had the same issue but someone on Stackoverflow suggested I make this change, which seems to have worked for me.
https://stackoverflow.com/questions/69548100/react-hook-form-and-react-select-not-working-as-expected
Anybody with a solution to this form reset issue?
@apexbb Are you resetting to
""? If so and still having issues, can you provide a minimal sandbox? Thansk!