【问题标题】:Typesafe onChange for React JS FormControl events用于 React JS FormControl 事件的类型安全 onChange
【发布时间】:2017-11-30 21:06:21
【问题描述】:

我有一个处理不断变化的文本字段的函数:

private handleNameChange(e: React.FormEvent<FormControl>) {
    const name = e.target.value;
    this.setState({ name });
    this.props.editGroupName(name);
}

这与 FormControl 的 onChange 相连:

<FormControl type='text' placeholder='Name' value={this.state.name} onChange={this.handleNameChange} />

但是,根据 TypeScript,这是错误的:

TS2339: Property 'value' does not exist on type 'EventTarget'.

我也尝试过使用currentTarget。我该如何解决这个问题?

【问题讨论】:

    标签: reactjs typescript types


    【解决方案1】:

    尝试将e: React.FormEvent&lt;FormControl&gt; 替换为e: React.FormEvent&lt;FormControlProps&gt;

    【讨论】:

    猜你喜欢
    • 2023-01-01
    • 2017-11-29
    • 2020-06-18
    • 2015-05-06
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2021-02-01
    相关资源
    最近更新 更多