我只想用我的KeyboardEvent来做这件事
var tag = evt.target.tagName.toLowerCase();
Run Code Online (Sandbox Code Playgroud)
虽然Event.target的类型为EventTarget,但它不会从Element继承.所以我必须这样投:
var tag = (<Element>evt.target).tagName.toLowerCase();
Run Code Online (Sandbox Code Playgroud)
这可能是因为有些浏览器没有遵循标准,对吧?TypeScript中与浏览器无关的正确实现是什么?
PS:我正在使用jQuery来捕获KeyboardEvent.
我在Typescript中构建类似React示例的东西.目标是让父母拥有一个州,并创建几个无状态子组件,将他们的点击次数传递回父级.
由于示例是在Javascript中,我不知道输入框事件的类型和onChange处理程序是什么......?我尝试了几种选择React.MouseEvent<HTMLInputElement>,但这只是猜测......
父组件 创建imageRows并传递处理程序:
render() {
<div>
<ImageRow key={el.url} onChange={this.onChange}/>
</div>
}
// what should the type of e be?
onChange(e:any){
}
Run Code Online (Sandbox Code Playgroud)
和ImageRow组件
export interface ImageRowProps {
genre: Array<number>
url: string
onChange : any // what is the type of the callback function?
}
export class ImageRow extends React.Component<ImageRowProps> {
render() {
return <div className="imagerow">
<input type="checkbox" key={index} onChange={this.props.onChange} defaultChecked={(num == 1)}/>
</div>
}
Run Code Online (Sandbox Code Playgroud)
编辑
类似的问题只显示事件类型,而不是处理程序类型.当我更改事件类型时:
onChange(e: React.FormEvent<HTMLInputElement>){
console.log(e.target.value)
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Property 'value' does not exist …Run Code Online (Sandbox Code Playgroud) 打字稿 2.3.4,反应 15.5.4 和反应引导 0.31.0。
我有一个FormControl,我想在用户按下回车键时做一些事情。
控制:
<FormControl
name="keyword"
type="text"
value={this.state.keyword}
onKeyPress={this.handleKeywordKeypress}
onChange={(event: FormEvent<FormControlProps>) =>{
this.setState({
keyword: event.currentTarget.value as string
});
}}
/>
Run Code Online (Sandbox Code Playgroud)
参数的定义应该handleKeywordKeypress是什么?
我可以这样定义:
handleKeywordKeypress= (e: any) =>{
log.debug("keypress: " + e.nativeEvent.code);
};
Run Code Online (Sandbox Code Playgroud)
这将被调用,它会打印kepress: Enter但类型应该是什么,e以便我可以将值与(什么?)进行比较以判断是否按下了 Enter 键。
带有 React 的 TypeScript 中的以下代码输出以下错误。
类型“EventTarget”上不存在属性“value”。
import React, { Component } from 'react';
class InputForm extends React.Component<any ,any> {
state = {
userInput: ''
};
handleUserInput = (e: React.FormEvent<HTMLInputElement>): void => {
this.setState({
userInput: e.target.value
});
}
// Working code from 42081549
// Not relevant to this project
update = (e: React.FormEvent<HTMLInputElement>): void => {
this.props.login[e.currentTarget.name] = e.currentTarget.value
}
submitMessage = (e: React.FormEvent<HTMLFormElement>): void => {
e.preventDefault();
this.props.sendUserMessage(this.state.userInput)
}
render() {
return (
<form className="chat-input-form" onSubmit={this.submitMessage}>
<input value={this.state.userInput} onChange={this.handleUserInput}/>
<button …Run Code Online (Sandbox Code Playgroud) 我在我的 svelte 项目中使用 Typescript,我需要为我的事件定义强类型。但我找不到任何方法可以做到这一点。
<script lang="ts">
const onKeyUp = (event: [type here]) => {
console.log({ event })
// const {target, keyCode} = event
}
</script>
<input type="text" on:keyup={onKeyUp} />
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我!
我试图将我的 keyup 事件与 TS 一起使用,但使用any|KeyboardEvent不起作用。
如何在 React 中输入事件?
Type '(e: KeyboardEvent) => void' is not assignable to type 'KeyboardEventHandler<HTMLInputElement>'.
Types of parameters 'e' and 'event' are incompatible.
Type 'KeyboardEvent<HTMLInputElement>' is missing the following properties from type 'KeyboardEvent': char, isComposing, DOM_KEY_LOCATION_LEFT, DOM_KEY_LOCATION_NUMPAD, and 15 more. TS2322
Run Code Online (Sandbox Code Playgroud)
<input
type="text"
placeholder="Search for claim ID"
onKeyUp={(e: KeyboardEvent) => {
console.info();
}}
/>
Run Code Online (Sandbox Code Playgroud)