简体   繁体   English

React/TypeScript:我应该如何处理这种组件组合方式?

[英]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.

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