Reactjs 如何键入属性';键';关于前置无状态组件
以下是无状态组件:Reactjs 如何键入属性';键';关于前置无状态组件,reactjs,typescript,preact,Reactjs,Typescript,Preact,以下是无状态组件: interface Props { children: JSX.Element; title: string; isHidden: boolean; } function TabContent({ children, title, isHidden }: Props): JSX.Element { return ( <section id={title} hidden={isHidden} >
interface Props {
children: JSX.Element;
title: string;
isHidden: boolean;
}
function TabContent({ children, title, isHidden }: Props): JSX.Element {
return (
<section
id={title}
hidden={isHidden}
>
{children}
</section>
);
}
export default TabContent;
但是Typescript抱怨类型道具上不存在属性“key”
我已经研究了如何在React中键入一个无状态组件,如下所示。它们都需要访问
React
模块,但我使用的是Preact..Preact与React的组件类似。因此,您可以使用组件类型
,函数组件
,函数组件
:
import { FunctionalComponent } from 'preact';
const TabContent: FunctionalComponent<Props> = ({ children, title, isHidden }) => (
<section
id={title}
hidden={isHidden}
>
{children}
</section>
);
从'preact'导入{FunctionalComponent};
常量选项卡内容:FunctionalComponent=({children,title,ishiden})=>(
{儿童}
);
我想你可以在道具中添加key?:string | number
(或任何有意义的类型)你是否尝试过使用preact本身的FunctionComponent
或ComponentType
?类似于const-TabContent:FunctionComponent=({children,title,isHidden})=>…
我像这样从“preact”导入{h,FunctionComponent}代码>。它起作用了。非常感谢。那我就贴出来作为答复
import { FunctionalComponent } from 'preact';
const TabContent: FunctionalComponent<Props> = ({ children, title, isHidden }) => (
<section
id={title}
hidden={isHidden}
>
{children}
</section>
);