I am using react-hook-form
with MUI components
. To use the Select
component, I have wrapped it in the Controller
as it can’t be registered directly. Everything is working but setValue
.
Here is the Code:
const { register, control, handleSubmit, setValue, formState: { errors } } = useForm();
useEffect(() => {
setValue('category',10)
}, [setValue]);
<Controller
name="category"
control={control}
render={({ field }) => (
<>
<TextField
{...field}
select
label={'Item Category'}
>
<MenuItem value={''} disabled>Select Category</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</TextField>
</>
)}
/>
I am expecting my Selector category to get updated on the initial load.
Update: One thing I found is that actually the value is being set on the load but not being rendered on the UI.
2
Answers
Found the answer myself:
Needed to set the
defaultValue
either in the controller or in theuseForm
default value.