Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript, 'onChange' is specified more than once, so this usage will be [duplicate]

I'm using material-ui, react-hook-form and Typescript in a react project, and when I try to add onChange in TextField VScode show me and error:

'onChange' is specified more than once, so this usage will be overwritten.ts(2783) formComponent.tsx(33, 15): This spread always overwrites this property.

formComponent.tsx :

        <TextField
          variant="standard"
          defaultValue={projectData.name}
          onChange={handleSubmit((data) => console.log(data))}
          {...register('name', { required: true })}
          error={!!errors.name}
        />
like image 789
andres martinez Avatar asked Sep 04 '25 17:09

andres martinez


1 Answers

You have to put onChange after the register spread

const textField = register('name', { required: true })

return (
    <input
        className="form-control"
        type="file"
        {...textField}
        onChange={(e) => {
          textField.onChange(e);
          handleSubmit(e);
     }}
    />
)

React hook form: How to can I use onChange on React Hook Form Version 7.0

like image 60
jsonderulo Avatar answered Sep 07 '25 12:09

jsonderulo