简体   繁体   English

类型“HTMLInputElement”上不存在属性“目标”。 TypeScript 反应。 TS2339

[英]Property 'target' does not exist on type 'HTMLInputElement'. TypeScript React. TS2339

The handleChange() function is called whenever the input element is changed, but can't seem to access event.target.value .每当更改输入元素时都会调用handleChange() function,但似乎无法访问event.target.value

Error Message错误信息

Property 'target' does not exist on type 'HTMLInputElement'.  TS2339

handleChange()处理变化()

handleChange = (event:HTMLInputElement) => {
    console.log(event);
    const { name, value } = event.target;
    this.setState({[name]: value});
};

Input Element输入元素

<input name='email' type='email' value={this.state.email} onChange={this.handleChange} required />

You have the wrong type on event .您在event上有错误的类型。 It's not an HTMLInputElement , it's an event .它不是HTMLInputElement ,而是一个事件 For change it's React.ChangeEvent<T> where T is the type of element you're using it on ( HTMLInputElement in your case), so:对于更改,它是React.ChangeEvent<T>其中T是您正在使用它的元素类型(在您的情况下为HTMLInputElement ),因此:

handleChange = (event: React.ChangeEvent<HTMLInputElement>) {
    // ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“X[]”上不存在属性“customProperty”。 打字稿反应。 TS2339 - Property 'customProperty' does not exist on type 'X[]'. Typescript React. TS2339 TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript Typescript TS2339:在Typescript + React + Redux应用程序中,类型&#39;IntrinsicAttributes&#39;上不存在属性&#39;queryType&#39;吗? - Typescript TS2339: Property 'queryType' does not exist on type 'IntrinsicAttributes' in Typescript + React + Redux app? 反应,Typescript,钩子 - 从钩子中解构数据,TS2339:类型上不存在属性“数据” - React, Typescript, Hooks - destructuring data from hook, TS2339: Property 'data' does not exist on type TS2339:移植到Typescript时,类型'typeof React'上不存在属性'PropTypes' - TS2339: Property 'PropTypes' does not exist on type 'typeof React' when porting to Typescript React typescript,类型“typeof TodoFilterItem”上不存在属性“propTypes”。 TS2339 - React typescript, Property 'propTypes' does not exist on type 'typeof TodoFilterItem'. TS2339 children.toArray() - TS2339:“ReactChild | 类型”上不存在属性“键” 反应片段 | ReactPortal'... - 反应 Typescript - children.toArray() - TS2339: Property 'key' does not exist on type 'ReactChild | ReactFragment | ReactPortal'... - React Typescript TS2339(TS)属性“状态”在类型上不存在 - TS2339 (TS) Property 'state' does not exist on type TS2339:“EventTarget”类型上不存在“最近”属性。 - 如何在 React 中获取原生 event.target? - TS2339: Property 'closest' does not exist on type 'EventTarget'. - How do I get native event.target in React? 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM