Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

333
Vistas
react-hook-form: using readyOnly in a select element

I am trying to use readOnly in a select element so the user is unable to change the value of the input field but when submiting the form I still need that value for the Api and I heard that the only way to do that in react-hook-form is to use readOnly instead of disabled, it worked in a normal input field but typescript gives an error in select element. This is the code:

interface dataProps {
  field_id: any;
  disabled: boolean;
}
<select
  readOnly={props.disabled}
  id={props.field_id}
  {...register(...)}
  defaultValue={...}
  onChange={...}
  >
  <option>
    {...}
  </option>
  renderList(...)
</select>

I tried to shorten the code as much as possible, this is the whole error I get:

Type '{ children: any[]; defaultValue: any; onChange: (e: ChangeEvent<HTMLSelectElement>) => void; onBlur: ChangeHandler; ref: RefCallBack; name: string; readOnly: true; id: any; }' is not assignable to type 'DetailedHTMLProps<SelectHTMLAttributes<HTMLSelectElement>, HTMLSelectElement>'.
  Property 'readOnly' does not exist on type 'DetailedHTMLProps<SelectHTMLAttributes<HTMLSelectElement>, HTMLSelectElement>'.

I read documentation of react-hook-form but I didn't see anything there about using readOnly, I landed on this link and when I tried to change the input there that has readOnly to select it gives the same error, is there a way to make that readOnly work or there is a workaround to make disabled save the data inside the handleSubmit?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The disabled attribute doesn't remove the field in react-hook-form when submitting. If you don't allow the user to edit the value, you can use it:

// category field is still registered even when set disabled.
<select {...register("category")} disabled>

In case you want to really remove the field from the submitted value, set disabled when calling register using RHF own API:

<input {...register("firstName", { disabled: true })}

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda