I want to set male
value as selected by default but its not working.
<select {...register("gender")} className="form-select input" id="gender">
<option value="Male" selected>{t("signUpPage.male")}</option>
<option value="Female">{t("signUpPage.female")}</option>
</select>
2
Answers
The issue is that React doesn’t work well with the selected attribute in tags because it handles state differently from plain HTML. Instead of using selected, you need to set the default value of the select element directly using the defaultValue or value prop.
Since you are using
register
it seems like you are usingreact-hook-form
. Withreact-hook-form
we can set default values with theuseForm
hook.So in your code, wherever you are calling
useForm
you would do this.I use
methods
here as a convention because I am not sure what you may be destructuring fromuseForm
, if anything.Also if you plan to take advantage of
isDirty
available onformState
it is suggested that you set default values for all the form properties.