reactjs React复选框事件和处理程序的 typescript 类型?

holgip5t  于 2023-01-30  发布在  React
关注(0)|答案(3)|浏览(160)

我正在用Typescript构建类似this React example的东西,目标是有一个有状态的父组件,它创建了几个无状态的子组件,这些子组件将它们的点击传递回父组件。
因为这个例子是用Javascript写的,所以我不知道输入框事件和onChange处理程序的类型是什么......我试过几个选项,比如React.MouseEvent<HTMLInputElement>,但那只是猜测......

    • 父组件**创建imageRows并传递处理程序:
render() {
  <div>
    <ImageRow key={el.url} onChange={this.onChange}/>
  </div>
 }
 // what should the type of e be?
 onChange(e:any){
 }

以及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>
}
    • 编辑**

类似的问题只显示事件类型,而不显示处理程序类型。当我更改事件类型时:

onChange(e: React.FormEvent<HTMLInputElement>){
    console.log(e.target.value)
}

我得到这个错误:

Property 'value' does not exist on type 'EventTarget'.
wkftcu5l

wkftcu5l1#

当有疑问时,让它通过使用箭头来为你推断。

答案

在您的情况下,e就是React.ChangeEvent<HTMLInputElement>

mzillmmw

mzillmmw2#

在我们的申请中,
控制台日志(事件目标值);//不工作(空值)
控制台.日志(事件.目标.已选中);//工作正常(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"
}
gxwragnw

gxwragnw3#

就这么办吧:

onChange = (event: Event) => {
      const { value } = event.target as unknown as { value: boolean, };
      setState(value);
};
  
<input type='radio' onChange={this.onChange} />

橡皮刷的线就会消失。

相关问题