解决React报错JSX element type does not have any construct or call signatures

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

解决React报错JSX element type does not have any construct or call signatures

Borislav Hadzhiev   2022-12-02 我要评论

总览

当我们试图将元素或react组件作为属性传递给另一个组件,但是属性的类型声明错误时,会产生"JSX element type does not have any construct or call signatures"错误。为了解决该错误,可以使用React.ElementType类型。

这里有个例子来展示错误是如何发生的。

// App.tsx
import React from 'react';
interface Props {
  comp: JSX.Element;
}
const Wrapper: React.FunctionComponent<Props> = props => {
  const {comp: Comp} = props;
  // ⛔️ JSX element type 'Comp' does not have any construct or call signatures.ts(2604)
  return (
    <div>
      <Comp name="James" />
    </div>
  );
};
const App: React.FunctionComponent = () => {
  const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  return (
    <div>
      <Wrapper comp={heading} />
    </div>
  );
};
export default App;

我们尝试将一个React组件作为属性传递给Wrapper组件,但我们将该React组件的类型声明为JSX.Element

React.ElementType

为了解决该错误,将属性的类型声明为React.ElementType

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们