Loại chính xác cho các sự kiện React là gì. Ban đầu tôi chỉ sử dụng anyvì mục đích đơn giản. Bây giờ, tôi đang cố gắng dọn dẹp mọi thứ và tránh sử dụng anyhoàn toàn.
Vì vậy, trong một hình thức đơn giản như sau:
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"
name='passw'
value={passw}
onChange={this.update}
/>
<button type="submit" onClick={this.submit} >
Submit
</button>
</form>
</div>
)
}
}
Tôi sử dụng loại nào ở đây làm loại sự kiện?
React.SyntheticEvent<EventTarget>dường như không hoạt động vì tôi gặp lỗi namevà valuekhông tồn tại trên target.
Câu trả lời tổng quát hơn cho tất cả các sự kiện sẽ thực sự được đánh giá cao.
Cảm ơn