我正在用 typewriter 写大约this React example个字.目标是让父组件具有状态,并创建几个无状态子组件,将其单击传递回父组件.
因为这个例子是用Javascript编写的,所以我不知道输入框事件和onChange处理程序的类型是什么...?我试过几个选项,比如React.MouseEvent<HTMLInputElement>
个,但那只是猜测...
Parent component
render() {
<div>
<ImageRow key={el.url} onChange={this.onChange}/>
</div>
}
// what should the type of e be?
onChange(e:any){
}
以及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>
}
EDIT
类似的问题只显示事件类型,而不是处理程序类型.当我更改事件类型时:
onChange(e: React.FormEvent<HTMLInputElement>){
console.log(e.target.value)
}
我得到了这个错误:
Property 'value' does not exist on type 'EventTarget'.