reactjs - Typy TypeScript pro události a handlery checkboxů v Reactu?
Vytvářím něco podobného tomuto příkladu v Reactu v TypeScriptu. Cílem je mít rodičovskou komponentu, která má stav, a ta vytváří několik bezstavových dětských komponent, které předávají své kliknutí zpět rodiči.
Protože je příklad v Javascriptu, nemám tušení, jaké typy mají události vstupního pole a handlery onChange...? Zkoušel jsem několik možností, například React.MouseEvent<HTMLInputElement>, ale to je jen hádání...
Rodičovská komponenta vytvoří imageRows a předá handler:
render() {
<div>
<ImageRow key={el.url} onChange={this.onChange}/>
</div>
}
// what should the type of e be?
onChange(e:any){
}
A komponenta ImageRow
export interface ImageRowProps {
genre: Array<number>
url: string
onChange : any // what is the type of the callback function?
}
export class ImageRow extends React.Component<ImageRowProps> {
render() {
return <div className="imagerow">
<input type="checkbox" key={index} onChange={this.props.onChange} defaultChecked={(num == 1)}/>
</div>
}
UPRAVIT
Podobná otázka zobrazuje pouze typ události, nikoli typ obsluhy. Když změním typ události:
onChange(e: React.FormEvent<HTMLInputElement>){
console.log(e.target.value)
}
Dostanu tuto chybu:
Property 'value' does not exist on type 'EventTarget'.
Odpověď č. 1
V případě pochybností nechte, aby to za vás odvodil pomocí šipky na pozici, např.
Odpověď
Ve vašem případě je e React.ChangeEvent<HTMLInputElement>.
Odpověď č. 2
V naší aplikaci,
console.log(event.target.value);/Nepracuje (prázdná hodnota)
console.log(event.target.checked);/Funguje správně ( true/false )
//../src/components/testpage2/index.tsx
import * as React from 'react';
import { TestInput } from '@c2/component-library';
export default class extends React.Component<{}, {}> {
state = {
model: {
isUserLoggedIn: true
}
};
onInputCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.value); // Not Working
console.log(event.target.checked); // Working
const field = event.target.name;
const model = this.state.model;
model[field] = event.target.checked;
return this.setState({model: model});
};
render() {
return (
<div>
<TestInput
name="isUserLoggedIn"
label="Is User LoggedIn : "
type="checkbox"
onChange={this.onInputCheckboxChange}
/>
</div>
);
}
}
//=============================================================//
import * as React from 'react';
//import * as cs from 'classnames';
export interface TestInputProps extends React.HTMLAttributes<HTMLInputElement> {
name: string;
label: string;
onChange: React.ChangeEventHandler<HTMLInputElement>;
placeholder?: string;
value?: string;
type?: string;
error?: string;
className?: string;
}
export const TestInput : React.SFC<TestInputProps> = ({ name, label, onChange, placeholder, value, type, className, error, ...rest }) => {
let wrapperClass:string = 'form-group';
if (error && error.length > 0) {
wrapperClass += " " + 'has-error';
}
return (
<div className={wrapperClass}>
<label htmlFor={name}>{label}</label>
<div className="field">
<input
type={type}
name={name}
className="form-control"
placeholder={placeholder}
value={value}
onChange={onChange}
/>
{error && <div className="alert alert-danger">{error}</div>}
</div>
</div>
);
}
TestInput.defaultProps ={
type: "text"
}
