简体   繁体   English

在使用 forwardRef 创建的 React 组件上输入错误

[英]Type error on a React component created with forwardRef

I'm creating an element(Card component) using forwardRef but types mismatch when using it.我正在使用 forwardRef 创建一个元素(卡片组件),但在使用它时类型不匹配。

Here is the code of Card component这是Card 组件的代码

export const Card = React.forwardRef((
  { fluidHeight, className, ...props }:
  { fluidHeight?:boolean, className?:string, props?:any }, ref?:any) => (
  <div
    {...props}
    ref={ref}
    className={clsx(
      "card card-custom gutter-b",
      { "card-height-fluid": fluidHeight },
      className
    )}
  />
));

Here is the code using Card component这是使用Card 组件的代码

<Card>
    <CardHeader title="List">
        <CardHeaderToolbar>
          <button
            type="button"
            className="btn btn-primary"
            onClick={UIProps.addButtonClick}
          >
            Add New
          </button>
        </CardHeaderToolbar>
      </CardHeader>
      <CardBody>
        <DevicesFilter />
        {UIProps.ids.length > 0 && <Grouping />}
        <DevicesTable />
      </CardBody>
 </Card>

In the above snippet using Card component, compiler show type error on and hoving over it show following error.在上面使用 Card 组件的片段中,编译器显示类型错误并将鼠标悬停在它上面显示以下错误。

Type '{ children: Element[];输入'{孩子:元素[]; }' has no properties in common with type 'IntrinsicAttributes & { fluidHeight?: boolean; }' 没有与类型 'IntrinsicAttributes & { fluidHeight?: boolean; 相同的属性。 className?: string;类名?:字符串; props?: any;道具?:任何; } & RefAttributes' } & RefAttributes'

I know it's a problem of forwardRef in typescript but don't know how to fix it.我知道这是打字稿中 forwardRef 的问题,但不知道如何解决。

Edit编辑

I found a hack like this我发现了一个这样的黑客

Card component卡片组件

export const Card = React.forwardRef((
  { children, fluidHeight, className, ...props }:
  {children:JSX.Element[], fluidHeight?:boolean, className?:string, props?:any }, ref?:any) => (
  <div
    {...props}
    ref={ref}
    className={clsx(
      "card card-custom gutter-b",
      { "card-height-fluid": fluidHeight },
      className
    )}
   >
    {/* {children} in case of forwardRef children elements are automatically passed from calling 
    component and should be used like this */}
  </div>
));

But as per my assumption in this case when I don't need to pass any children elements for rendering inside Card component, I should use something else than forwardRef but don't know what :)但是根据我在这种情况下的假设,当我不需要传递任何子元素来在 Card 组件内部进行渲染时,我应该使用 forwardRef 以外的其他东西,但不知道是什么:)

Please Help!请帮忙! Thanks谢谢

For children type, you shouldn't use JSX.Element or JSX.Element[] .对于子类型,您不应使用JSX.ElementJSX.Element[] Usually the convention is to use React.ReactNode .通常约定是使用React.ReactNode It includes the 2 above types and all other valid types for children like string or null .它包括上述 2 种类型和所有其他有效的儿童类型,如stringnull

So I'd change the props definition to:所以我将道具定义更改为:

type YourProps = { fluidHeight?:boolean, className?:string, props?:any };
type CardProps = React.PropsWithChildren<YourProps>;

export const Card = React.forwardRef((
  { fluidHeight, className, ...props }: CardProps, ref?:any) => (
  <div
    {...props}
    ref={ref}
    className={clsx(
      "card card-custom gutter-b",
      { "card-height-fluid": fluidHeight },
      className
    )}
  />
));

where React.PropsWithChildren<T> is a utility type that includes children as React.ReactNode type.其中React.PropsWithChildren<T>是一个实用程序类型,它包含子React.ReactNode作为React.ReactNode类型。 Here is the definition这是定义

type PropsWithChildren<P> = P & { children?: ReactNode };

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

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