React事件的正确类型是什么.最初我只是any为了简单起见而使用.现在,我正在努力清理并避免any完全使用.
所以用这样一个简单的形式:
export interface LoginProps {
login: {
[k: string]: string | Function
uname: string
passw: string
logIn: Function
}
}
@inject('login') @observer
export class Login extends Component<LoginProps, {}> {
update = (e: React.SyntheticEvent<EventTarget>): void => {
this.props.login[e.target.name] = e.target.value
}
submit = (e: any): void => {
this.props.login.logIn()
e.preventDefault()
}
render() {
const { uname, passw } = this.props.login
return (
<div id='login' >
<form>
<input
placeholder='Username'
type="text"
name='uname'
value={uname}
onChange={this.update}
/>
<input
placeholder='Password'
type="password" …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)