typescript 입력 onchange event.target.value
내 반응 및 타이프 스크립트 앱에서 다음을 사용 onChange={(e) => data.motto = (e.target as any).value}
합니다..
클래스의 타이핑을 올바르게 정의하는 방법은 any
무엇입니까?
export interface InputProps extends React.HTMLProps<Input> {
...
}
export class Input extends React.Component<InputProps, {}> {
}
내가 넣으면 target: { value: string };
:
ERROR in [default] /react-onsenui.d.ts:87:18
Interface 'InputProps' incorrectly extends interface 'HTMLProps<Input>'.
Types of property 'target' are incompatible.
Type '{ value: string; }' is not assignable to type 'string'.
일반적으로 이벤트 핸들러는를 사용해야합니다 e.currentTarget.value
. 예 :
onChange = (e: React.FormEvent<HTMLInputElement>) => {
const newValue = e.currentTarget.value;
}
그 이유는 여기에서 읽을 수 있습니다 ( "Revert"Make SyntheticEvent.currentTarget. " ).
UPD : @ roger-gusmao가 언급했듯이 ChangeEvent
양식 이벤트를 입력하는 데 더 적합합니다.
onChange = (e: React.ChangeEvent<HTMLInputElement>)=> {
const newValue = e.target.value;
}
TypeScript에서 사용하는 올바른 방법은
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} />
...
);
}
전체 수업을 따르십시오. 이해하는 것이 좋습니다.
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;
as HTMLInputElement
나를 위해 일해
target
당신이 추가하려고은 InputProps
동일하지 않습니다 target
에있는 당신이 원하는React.FormEvent
So, the solution I could come up with was, extending the event related types to add your target type, as:
interface MyEventTarget extends EventTarget {
value: string
}
interface MyFormEvent<T> extends React.FormEvent<T> {
target: MyEventTarget
}
interface InputProps extends React.HTMLProps<Input> {
onChange?: React.EventHandler<MyFormEvent<Input>>;
}
Once you have those classes, you can use your input component as
<Input onChange={e => alert(e.target.value)} />
without compile errors. In fact, you can also use the first two interfaces above for your other components.
lucky i find a solution. you can
import { ChangeEvent } from 'react';
and then write code like: e:ChangeEvent<HTMLInputElement>
Here is a way with ES6 object destructuring, tested with TS 3.3.
This example is for a text input.
name: string = '';
private updateName({ target }: { target: HTMLInputElement }) {
this.name = target.value;
}
This is when you're working with a FileList
Object:
onChange={(event: React.ChangeEvent<HTMLInputElement>): void => {
const fileListObj: FileList | null = event.target.files;
if (Object.keys(fileListObj as Object).length > 3) {
alert('Only three images pleaseeeee :)');
} else {
// Do something
}
return;
}}
function handle_change(
evt: React.ChangeEvent<HTMLInputElement>
): string {
evt.persist(); // This is needed so you can actually get the currentTarget
const inputValue = evt.currentTarget.value;
return inputValue
}
And make sure you have "lib": ["dom"]
in your tsconfig
.
참고URL : https://stackoverflow.com/questions/40676343/typescript-input-onchange-event-target-value
'Development Tip' 카테고리의 다른 글
코드에서 android : versionName 매니페스트 요소 가져 오기 (0) | 2020.11.06 |
---|---|
하위 프로세스 변경 디렉터리 (0) | 2020.11.06 |
iPhone에서 HTML 구문 분석 (0) | 2020.11.06 |
모든 배열이 C #에서 구현하는 인터페이스는 무엇입니까? (0) | 2020.11.06 |
How to bring a gRPC defined API to the web browser (0) | 2020.11.05 |