TypeScript,'onChange'被指定多次,所以这个用法将是

and*_*nez 21 typescript reactjs material-ui

我在 React 项目中使用material-ui、react-hook-form 和 Typescript,当我尝试在 TextField 中添加 onChange 时,VScode 显示错误:

'onChange' 被指定多次,因此此用法将被覆盖。ts(2783) formComponent.tsx(33, 15):此展开始终覆盖此属性。

formComponent.tsx

        <TextField
          variant="standard"
          defaultValue={projectData.name}
          onChange={handleSubmit((data) => console.log(data))}
          {...register('name', { required: true })}
          error={!!errors.name}
        />
Run Code Online (Sandbox Code Playgroud)

loc*_*red 56

您必须在注册扩展后进行 onChange

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

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

React hook 表单:如何在 React Hook Form 版本 7.0 上使用 onChange


归档时间:

查看次数:

20844 次

最近记录:

3 年,11 月 前