radio buttons are not selected by default when using with useController in NextJs app but working in react app #11563
-
I've simple nextjs page in which I'm using Radios are only selected by default when using with NextJS example with react-hook-form"use client";
import { useController, Control, useForm } from "react-hook-form";
function TypeSelector({ control }: { control: Control<any> }) {
const { field } = useController({
control,
name: "type",
defaultValue: "text",
});
return (
<div>
<p>useController::Default type should be text</p>
<label>
<input type="radio" {...field} value={"media"} />
Media
</label>
<label>
<input type="radio" {...field} value={"text"} />
Text
</label>
</div>
);
}
export default function Home() {
const { control, register } = useForm({
defaultValues: {
type: "text",
gender: "male",
},
});
return (
<div>
<TypeSelector control={control} />
<p>register::Default gender should be male</p>
<label>
<input type="radio" {...register("gender")} value={"male"} />
Male
</label>
<label>
<input type="radio" {...register("gender")} value={"female"} />
Female
</label>
</div>
);
} ![]() But same thing is working fine in react. ReactJS example with react-hook-formimport { useController, Control, useForm } from "react-hook-form";
function TypeSelector({ control }: { control: Control<any> }) {
const { field } = useController({
control,
name: "type",
defaultValue: "text",
});
return (
<div>
<p>useController::Default type should be text</p>
<label>
<input type="radio" {...field} value={"media"} />
Media
</label>
<label>
<input type="radio" {...field} value={"text"} />
Text
</label>
</div>
);
}
export default function Home() {
const { control, register } = useForm({
defaultValues: {
type: "text",
gender: "male",
},
});
return (
<div>
<TypeSelector control={control} />
<p>register::Default gender should be male</p>
<label>
<input type="radio" {...register("gender")} value={"male"} />
Male
</label>
<label>
<input type="radio" {...register("gender")} value={"female"} />
Female
</label>
</div>
);
} ![]() |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi,
First of all, the ReactJS example with react-hook-form uses "react-hook-form": "7.4.1" whereas the next js version uses "react-hook-form": "7.50". If you update to the latest version, you can observe the same behaviour; the first radio button doesn't set the default value. Probably, it looks like the old react-hook-form's controller updates the
You can simply add the <input type="radio" {...field} value={"media"} defaultChecked /> Also, you don't need to set defaultValue again because you already give defaultValues in the useForm. const { field } = useController({
control,
name: "type",
}); Here is the next.js version codesandbox. |
Beta Was this translation helpful? Give feedback.
Hi,
First of all, the ReactJS example with react-hook-form uses "react-hook-form": "7.4.1" whereas the next js version uses "react-hook-form": "7.50". If you update to the latest version, you can observe the same behaviour; the first radio button doesn't set the default value.
Probably, it looks like the old react-hook-form's controller updates the
checked
likeregister()
by refs.You can simply add the
defaultChecked
orchecked
prop, but not both.Also, you don't need to set defaultValue again because you already give defaultV…