reactjs - 如何在打字稿中为反应组件指定类型?
问题描述
我想将一个反应自定义组件作为属性发送到另一个组件
event-logistics.tsx // component name
import { DateIcon } from "../../ui/icons/DateIcon";
import LogisticsItem from "./logistics-item";
<LogisticsItem icon={DateIcon}>
<time>{humanReadableDate}</time>
</LogisticsItem>
它在图标属性上给了我这个错误:
JSX element type 'Icon' does not have any construct or call signatures.ts(2604)
const Icon: (({ height, width, color, ...props }: SVGProps<SVGSVGElement>) => Element) | undefined
如您所见,我想将作为反应 SVG 组件的 'DateIcon' 发送到图标属性上的“LogisticsItem”组件
logistics-item.tsx // component name
import style from "./logistics-item.module.css";
import { DetailedHTMLProps, LiHTMLAttributes, SVGProps } from "react";
export const LogisticsItem: React.FunctionComponent<
DetailedHTMLProps<LiHTMLAttributes<HTMLLIElement>, HTMLLIElement> & {
icon?: ({
height,
width,
color,
...props
}: SVGProps<SVGSVGElement>) => Element;
}
> = ({ title, children, icon, ...rest }) => {
const Icon = icon;
return (
<li className={style.item}>
<span className={style.icon}>
<Icon />
</span>
<span className={style.content}>{children}</span>
</li>
);
};
export default LogisticsItem;
解决方案
只需使用FC<SVGProps<SVGSVGElement>>
而不是
{
icon?: ({
height,
width,
color,
...props
}: SVGProps<SVGSVGElement>) => Element
考虑这个例子:
import React, { FC, createElement } from 'react';
import { DetailedHTMLProps, LiHTMLAttributes, SVGProps } from "react";
export const LogisticsItem: React.FunctionComponent<
DetailedHTMLProps<LiHTMLAttributes<HTMLLIElement>, HTMLLIElement> & {
icon: FC<SVGProps<SVGSVGElement>>;
}
> = ({ title, children, icon, ...rest }) => {
const Icon = icon;
return (
<li>
<span>
<Icon />
</span>
<span >{children}</span>
</li>
);
};
const DateIcon = ({ height = "24px", width = "24px", color = "black", ...props }: React.SVGProps<SVGSVGElement>) =>
(<svg xmlns="w3.org/2000/svg" width={width} height={height} fill="none" viewBox="0 0 24 24" stroke="currentColor" {...props} > <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" /> </svg>)
const result = <LogisticsItem icon={DateIcon} />
没有错误
推荐阅读
- html - 为什么我的下拉内容在 Safari 上不起作用?
- javascript - 是否可以对跨域 iframe 进行沙箱处理,然后在单击沙箱时将其删除?
- c# - Dapper OracleMappingType.RefCursor 方向:ParameterDirection.Input 不起作用
- c - 您将如何更改具有缓冲区溢出的全局变量?
- python - 将结果导出到 excel 文件 python BeautifulSoup
- sql - 在 Knex.js 查询中使用非重音 postgres 扩展
- excel - Excel 将 DOB 转换为 DOB 相关的到期日期
- mysql - 如何在没有关系的情况下连接两个表,只从一个表中获取一个值并将其粘贴到另一个表中?
- arrays - 在 PowerShell 中有一种方法可以使用字符串的子字符串返回数组的索引
- java - Android Studio - GoogleAPIClient 已弃用修复