Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!