简体   繁体   English

与 Typescript 反应 - 键入 ChangeEvent 时出现问题

[英]React with Typescript - problem with typing ChangeEvent



I've been learning react with typescript for a while and ran into a problem.我一直在学习对 typescript 的反应一段时间并遇到了问题。
My prop onChangeHandler takes a function to change properties in values in formik.我的道具 onChangeHandler 采用 function 来更改 formik 中值的属性。

<Formik<FormModel>
        initialValues={{
            favorite: false
            ...
        }}

        onSubmit={(values) => {
            ...
        }}

        validate={(values) => {
            ...
        }}
    >
        {({ handleSubmit, values, handleChange, setFieldValue, errors }) => {

            const checkboxOnChange = (
                e: ChangeEvent<HTMLInputElement>
            ) => {
                setFieldValue("favorite", e.target.checked);
            };

            return (
                <Form.Wrapper onSubmit={handleSubmit}>
                    
                   ...

                    <FormField
                        ...
                        inputtype="checkbox"
                        inputname="favorite"
                        inputchecked={values.favorite}
                        onChangeHandler={checkboxOnChange}
                    />

                    <Form.Button type="submit">Add task</Form.Button>
                </Form.Wrapper>
            );
        }}
    </Formik>

In my FormField compoentent file is:在我的 FormField 组件文件中是:

type FormFieldTypes = {
fieldtype: "text" | "textarea" | "date" | "select" | "checkbox";
...
inputtype: string;
inputname: string;
inputchecked?: boolean;
onChangeHandler: (
    e:
        | React.ChangeEvent<HTMLInputElement>
        | React.ChangeEvent<HTMLTextAreaElement>
        | React.ChangeEvent<HTMLSelectElement>
) => void;
...
};


 const FormField = ({
  fieldtype,
  inputtype,
  inputname,
  inputvalue,
  inputchecked,
  onChangeHandler,
  ...
 }: FormFieldTypes) => {

  const input = () => {
    switch (fieldtype) {

        ...

        case "checkbox":
            return (
                <Input
                    name={inputname}
                    type={inputtype}
                    checked={inputchecked}
                    onChange={onChangeHandler}
                />
            );

        default:
            return null;
    }
};

return (
    <Field fieldtype={fieldtype} ...>

        {input()}
        ...
    </Field>
);
};

export default FormField;

` `

my question is why am I getting error "Type '(e: ChangeEvent <HTMLInputElement>) => void' is not assignable to type '(e: ChangeEvent <HTMLInputElement> | ChangeEvent <HTMLTextAreaElement> | ChangeEvent <HTMLSelectElement>) => void'. " if I have implemented ChangeEvent <HTMLInputElement> and if checkbox is input and what do i need to change in the code to make it work.我的问题是为什么我收到错误“Type '(e: ChangeEvent <HTMLInputElement>) => void' is notassignable to type '(e: ChangeEvent <HTMLInputElement> | ChangeEvent <HTMLTextAreaElement> | ChangeEvent <HTMLSelectElement>) => void '。” 如果我已经实现了 ChangeEvent <HTMLInputElement> 并且如果输入了复选框,我需要在代码中进行哪些更改才能使其工作。

ok, i don't know what is the best solution for this, but i think there's some logic we can sort out first.好的,我不知道什么是最好的解决方案,但我认为我们可以先理清一些逻辑。

The three options are not三个选项都不

(
    e:
        | React.ChangeEvent<HTMLInputElement>
        | React.ChangeEvent<HTMLTextAreaElement>
        | React.ChangeEvent<HTMLSelectElement>
) => void

which is only one option, the real three options that you want to supply to it is:这只是一个选项,您要提供给它的真正三个选项是:

  (React.ChangeEvent<HTMLInputElement>) => void |
  (React.ChangeEvent<HTMLTextAreaElement>) => void |
  (React.ChangeEvent<HTMLSelectElement>) => void

I think this is the main issue i found.我认为这是我发现的主要问题。 Basically you have three choices of events instead of one function with three different inputs.基本上,您有三种事件选择,而不是一个具有三个不同输入的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM