reactjs - Flow 给出错误“无法将 [value] 分配给 this.[property],因为 [class] 中缺少属性 [property]”
问题描述
这有效:
// @flow
import React, {Component} from 'react';
type Props = {};
class Delete extends Component<Props> {
targetElement: null | winndow.HTMLInputElement;
constructor(props: Props) {
super(props);
this.targetElement = null; // initialize to null
}
handleClick = (e: SyntheticEvent<> & {target: window.HTMLInputElement}) => {
this.targetElement = (e.target: window.HTMLInputElement).value;
};
render() {
return (
<input onClick={this.handleClick} value="hello" />
);
}
}
export default Delete;
但这不是:
...
handleClick = (e: SyntheticEvent<> & {target: window.HTMLInputElement}) => {
this.targetElement = e.target.value;
};
...
错误信息是:Cannot get e.target.value because property value is missing in EventTarget
。
1)我已经输入了“目标”属性window.HTMLInputElement
,它有一个 value 属性,那么为什么 Flow 坚持认为 EventTarget 中缺少属性值(为什么是 EventTarget?)
首先,在我添加 Flow 注释之前,代码运行良好,并且没有关于缺少属性的控制台错误。
我真的很困惑这是如何工作的 - 我已经阅读了 this和this,似乎有大量的解决方法/解决方案,但我不确定它们为什么会起作用。此外,这些都是 2-3 岁,我不确定它们现在是否仍然相关。我是 Flow 的新手,但我真的很想掌握它!
解决方案
推荐阅读
- android-studio - 无法在 Android Studio 中安装插件
- node.js - 如何使用统一拓扑设置自动重新连接
- react-native - 即使在 React 本机的 iOS 设备中拒绝许可,我也能够从照片库中获取图像
- sql - 如何在 postgreSQL 中仅使用 array_agg 获得多个结果?
- flutter - 在调试方法中引用所有公共属性
- amazon-web-services - EC2 实例指向的文件夹未在 AMI 快照中打开
- javascript - 失败的道具类型:道具`options`在`signupCheckBoxes`中标记为必填,但其值为`undefined`
- html - 从 appscript 中基于 html 的对象抛出的格式错误的 html 错误
- java - 当奴隶尝试联系詹金斯时,詹金斯返回 403 禁止
- xml - 如何添加新的