相关疑难解决方法(0)

打字稿:反应事件类型

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)

javascript typescript reactjs

91
推荐指数
8
解决办法
10万
查看次数

在 React TypeScript 中输入输入的 keyup

我试图将我的 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)

javascript typescript reactjs

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×2

reactjs ×2

typescript ×2