Typescript: React event types

前端 未结 8 869
故里飘歌
故里飘歌 2020-11-27 11:50

What is the correct type for React events. Initially I just used any for the sake of simplicity. Now, I am trying to clean things up and avoid use of any<

相关标签:
8条回答
  • 2020-11-27 12:11

    I think the simplest way is that:

    type InputEvent = React.ChangeEvent<HTMLInputElement>;
    type ButtonEvent = React.MouseEvent<HTMLButtonElement>;
    
    update = (e: InputEvent): void => this.props.login[e.target.name] = e.target.value;
    submit = (e:  ButtonEvent): void => {
        this.props.login.logIn();
        e.preventDefault();
    }
    
    0 讨论(0)
  • 2020-11-27 12:18

    To combine both Nitzan's and Edwin's answers, I found that something like this works for me:

    update = (e: React.FormEvent<EventTarget>): void => {
        let target = e.target as HTMLInputElement;
        this.props.login[target.name] = target.value;
    }
    
    0 讨论(0)
  • 2020-11-27 12:20

    The SyntheticEvent interface is generic:

    interface SyntheticEvent<T> {
        ...
        currentTarget: EventTarget & T;
        ...
    }
    

    (Technically the currentTarget property is on the parent BaseSyntheticEvent type.)

    And the currentTarget is an intersection of the generic constraint and EventTarget.
    Also, since your events are caused by an input element you should use the ChangeEvent (in definition file, the react docs).

    Should be:

    update = (e: React.ChangeEvent<HTMLInputElement>): void => {
        this.props.login[e.currentTarget.name] = e.currentTarget.value
    }
    

    (Note: This answer originally suggested using React.FormEvent. The discussion in the comments is related to this suggestion, but React.ChangeEvent should be used as shown above.)

    0 讨论(0)
  • 2020-11-27 12:23

    you can do like this in react

    handleEvent = (e: React.SyntheticEvent<EventTarget>) => {
      const simpleInput = (e.target as HTMLInputElement).value;
      //for simple html input values
      const formInput = (e.target as HTMLFormElement).files[0];
      //for html form elements
    }
    
    0 讨论(0)
  • 2020-11-27 12:25

    I have the following in a types.ts file for html input, select, and textarea:

    export type InputChangeEventHandler = React.ChangeEventHandler<HTMLInputElement>
    export type TextareaChangeEventHandler = React.ChangeEventHandler<HTMLTextAreaElement>
    export type SelectChangeEventHandler = React.ChangeEventHandler<HTMLSelectElement>
    

    Then import them:

    import { InputChangeEventHandler } from '../types'
    

    Then use them:

    
    const updateName: InputChangeEventHandler = (event) => {
      // Do something with `event.currentTarget.value`
    }
    const updateBio: TextareaChangeEventHandler = (event) => {
      // Do something with `event.currentTarget.value`
    }
    const updateSize: SelectChangeEventHandler = (event) => {
      // Do something with `event.currentTarget.value`
    }
    

    Then apply the functions on your markup (replacing ... with other necessary props):

    <input onChange={updateName} ... />
    <textarea onChange={updateName} ... />
    <select onChange={updateSize} ... >
      // ...
    </select>
    
    0 讨论(0)
  • 2020-11-27 12:26

    for update: event: React.ChangeEvent for submit: event: React.FormEvent for click: event: React.MouseEvent

    0 讨论(0)
提交回复
热议问题