[英]React/TypeScript: How should I handle this style of component composition?
I'm very new to typescript
so please bear with me.我对
typescript
很陌生,所以请多多包涵。
I'm trying to convert this react
component:我正在尝试转换此
react
组件:
interface ButtonProps {...}
const Button: React.FC<ButtonProps> = ({
children,
href,
value
as = 'button',
...props
}) => {
const Element = href ? 'a' : as;
// ...
return (
<Element {...props}>
<span>{value || children}</span>
</Element>
);
};
But I'm getting the following error on the <Element>
element:但我在
<Element>
元素上收到以下错误:
JSX element type 'Element' does not have any construct or call signatures.
I can satisfy the compiler by typing Element
with any
but that feels dirty.我可以通过键入
any
但感觉很脏的Element
来满足编译器。
How should I be approaching this pattern from a TS
point of view?从
TS
的角度来看,我应该如何处理这种模式?
Thanks!谢谢!
Use ElementType
使用
ElementType
interface ButtonProps {
...
as?: ElementType
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.