键入onChange事件时出现Typescript错误

时间:2019-06-26 06:49:58

标签: reactjs typescript material-ui

我像下面这样声明了System.__ComObject

state

我有类似的元素

const [updatedStep, updateStepObj] = useState(
    panel === 'add'
        ? new Step()
        : {
                ...selectedStep
          }
);

我遇到 <TextField label="Title" value={updatedStep.title} onChange={(e: React.ChangeEvent<HTMLInputElement>) => updateStepObj({ ...updatedStep, title: e.currentTarget.value }) } /> 事件的Typescript错误。如何摆脱onChange

enter image description here

1 个答案:

答案 0 :(得分:1)

您必须为onChange方法放置与声明TextField相同类型的数据,所以就像这样:

<TextField
   label="Title"
   value={updatedStep.title}
   onChange={(e: React.FormEvent<HTMLInputElement>, newValue?: string) =>
       updateStepObj({ ...updatedStep, title: e.currentTarget.value })
   }
/>

注意:要忘记这种混乱,在将方法添加到事件之前,最好要做的就是用鼠标悬停在鼠标上,它会告诉您Tipado。然后,编写相同的代码并执行所需的操作。

无论如何,如果您想要更准确的信息,请在CodeSandbox中添加一个演示,然后看一看:)