Typescript input onchange event.target.value

后端 未结 11 854
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 21:25

In my react and typescript app, I use: onChange={(e) => data.motto = (e.target as any).value}.

How do I correctly define the typings for the class, s

相关标签:
11条回答
  • 2020-11-28 22:04

    as HTMLInputElement works for me

    0 讨论(0)
  • 2020-11-28 22:05

    Thanks @haind

    Yes HTMLInputElement worked for input field

    //Example
    var elem = e.currentTarget as HTMLInputElement;
    elem.setAttribute('my-attribute','my value');
    elem.value='5';
    

    This HTMLInputElement is interface is inherit from HTMLElement which is inherited from EventTarget at root level. Therefore we can assert using as operator to use specific interfaces according to the context like in this case we are using HTMLInputElement for input field other interfaces can be HTMLButtonElement, HTMLImageElement etc.

    For more reference you can check other available interface here

    • Web API interfaces by Mozilla
    • Interfaces in External Node Modules by Microsoft
    0 讨论(0)
  • 2020-11-28 22:07

    When using Child Component We check type like this.

    Parent Component:

    export default () => {
    
      const onChangeHandler = ((e: React.ChangeEvent<HTMLInputElement>): void => {
        console.log(e.currentTarget.value)
      }
    
      return (
        <div>
          <Input onChange={onChangeHandler} />
        </div>
      );
    }
    

    Child Component:

    type Props = {
      onChange: (e: React.ChangeEvent<HTMLInputElement>) => void
    }
    
    export Input:React.FC<Props> ({onChange}) => (
      <input type="tex" onChange={onChange} />
    )
    
    0 讨论(0)
  • 2020-11-28 22:10

    lucky i find a solution. you can

    import { ChangeEvent } from 'react';

    and then write code like: e:ChangeEvent<HTMLInputElement>

    0 讨论(0)
  • 2020-11-28 22:11

    the correct way to use in TypeScript is

      handleChange(e: React.ChangeEvent<HTMLInputElement>) {
        // No longer need to cast to any - hooray for react!
        this.setState({temperature: e.target.value});
      }
    
      render() {
            ...
            <input value={temperature} onChange={this.handleChange} />
            ...
        );
      }
    

    Follow the complete class, it's better to understand:

    import * as React from "react";
    
    const scaleNames = {
      c: 'Celsius',
      f: 'Fahrenheit'
    };
    
    
    interface TemperatureState {
       temperature: string;
    }
    
    interface TemperatureProps {
       scale: string;
    
    }
    
    class TemperatureInput extends React.Component<TemperatureProps, TemperatureState> {
      constructor(props: TemperatureProps) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {temperature: ''};
      }
    
      //  handleChange(e: { target: { value: string; }; }) {
      //    this.setState({temperature: e.target.value});  
      //  }
    
    
      handleChange(e: React.ChangeEvent<HTMLInputElement>) {
        // No longer need to cast to any - hooray for react!
        this.setState({temperature: e.target.value});
      }
    
      render() {
        const temperature = this.state.temperature;
        const scale = this.props.scale;
        return (
          <fieldset>
            <legend>Enter temperature in {scaleNames[scale]}:</legend>
            <input value={temperature} onChange={this.handleChange} />
          </fieldset>
        );
      }
    }
    
    export default TemperatureInput;
    
    0 讨论(0)
提交回复
热议问题