繁体   English   中英

如何在 React/Typescript 中使用导入的类型

[英]How to use imported type in React/Typescript

import { IoLogOutOutline } from 'react-icons/io5';    
import { IconType } from 'react-icons';

如何使用导入的类型“图标类型”?

(alias) type IconType = (props: IconBaseProps) => JSX.Element

将图标类型设置为 JSX.Element 时,我可以将图标作为道具传递。

type LinkProps = {
    to: string;
    icon?: JSX.Element;
};

<Link to="logout" icon={<IoLogOutOutline />} />

但是,如果我将其设置为“图标类型”,则会返回错误:

"Type 'Element' is not assignable to type 'IconType'.
  Type 'ReactElement<any, any>' provides no match for the signature '(props: IconBaseProps): Element'.ts(2322)"

IconType是一个 function 类型,它返回一些道具并期望一个元素,因此请尝试将 function 传递给图标道具icon={() => <IoLogOutOutline />}

import { IoLogOutOutline } from 'react-icons/io5';    
import { IconType } from 'react-icons';

type LinkProps = {
    to: string;
    icon?: IconType;
};

function Link(props: LinkProps) {
    return ...;
}

<Link to="logout" icon={() => <IoLogOutOutline />} />

暂无
暂无

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

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