如何使用功能组件和打字稿转发参考包装的组件

时间:2020-02-27 21:21:47

标签: reactjs react-bootstrap react-forwardref

ReactJS的Typescript中的forwardRef出现问题。

我的目标是包装引导FormControl并为其提供一些简单的逻辑,然后在其上进行构建。我知道FormControl将其引用转发到输入元素。

现在如何将组件中的引用转发到FormControl正在转发的该引用。我的代码现在看起来像这样:

import React from "react";
import { Form } from 'react-bootstrap';

export type FormSimpleTextProps = {
    name: string;
    placeholder: string;
    value: string;
    updated: boolean;
    editable: boolean;
    onChange: (name: string, newValue: string) => void;
};

export const FormSimpleText = React.forwardRef(
    (props: FormSimpleTextProps, ref: React.Ref<HTMLInputElement>) => {

        const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
            console.log(e);
        }

        return (
            <Form.Control

               ref={ref}
                name={props.name}
                value={props.value}
                onChange={onChangeHandler}
                className={props.updated ? "selected" : ""}
                plaintext={!props.editable}
                readOnly={!props.editable}
                required
                type="text"
                placeholder={props.placeholder}
            />
        );
    }
);

在ref = {ref}的行中,我遇到了错误:

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>>): FormControl<"input">', gave the following error.
    Type '((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
      Type '(instance: HTMLInputElement | null) => void' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
        Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string & ((instance: HTMLInputElement | null) => void)'.
          Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string'.
  Overload 2 of 2, '(props: ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>, context?: any): FormControl<"input">', gave the following error.
    Type '((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
      Type '(instance: HTMLInputElement | null) => void' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
        Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string & ((instance: HTMLInputElement | null) => void)'.
          Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string'.  TS2769

    20 |         return (
    21 |             <Form.Control
  > 22 |                 ref={ref}
       |                 ^
    23 |                 name={props.name}
    24 |                 value={props.value}
    25 |                 onChange={onChangeHandler}

我只是无法根据错误消息弄清楚这里出了什么问题。对我来说太复杂了。有人可以帮忙这个例子吗?

0 个答案:

没有答案