search

Home  >  Q&A  >  body text

React input boolean value in default value

I have a controlled checkbox input and I am supposed to set the default value on request but I am getting the error "Type 'boolean' is not assignable to type 'string | number | readonly string[] | undefined'". Maybe you know how to avoid this?

<input
   {...register("car")}
   type="checkbox"
   defaultValue={data.car ? true : false}
/>

P粉148782096P粉148782096448 days ago515

reply all(1)I'll reply

  • P粉019353247

    P粉0193532472023-09-11 12:46:05

    You can use the defaultChecked attribute on input elements.

    For example:

    <input
      {...register("car")}
      type="checkbox"
      defaultChecked={data.car ? true : false}
    />

    reply
    0
  • Cancelreply