[英]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.