我写了一些代码:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
我得到一个错误:
JSX元素类型Elem没有任何构造或调用签名
这是什么意思?
我写了一些代码:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
我得到一个错误:
JSX元素类型Elem没有任何构造或调用签名
这是什么意思?
当前回答
在我的例子中,我在类型定义中缺少new。
some-js-component.d。ts文件:
import * as React from "react";
export default class SomeJSXComponent extends React.Component<any, any> {
new (props: any, context?: any)
}
在tsx文件中,我试图导入非类型化组件:
import SomeJSXComponent from 'some-js-component'
const NewComp = ({ asdf }: NewProps) => <SomeJSXComponent withProps={asdf} />
其他回答
这是语法的问题
下面是一个使用TSX的例子:
const SomeMadeComponent = (props: { someText: string }) => {
const { someText} = props;
return (
<div>
<p>{someText}</p>
</div>
);
};
你可以像使用普通组件一样使用它:
<SomeMadeComponent someText='把你的文本放在这里' />
当我从JSX转换到TSX时,我们保留了一些库作为js/ JSX,并将其他库转换为ts/ TSX,我几乎总是忘记更改TSX\ ts文件中的js/ JSX导入语句
import * as ComponentName(组件名);
to
import ComponentName from "ComponentName"
如果从TSX调用旧的JSX (React.createClass)样式组件,则使用
变量
就我而言,我使用的是React。ReactNode作为功能组件的类型,而不是React。FC型。
准确地说,在这个组件中:
export const PropertiesList: React。FC = (props: any) => { Const列表:string[] = [ 《后果:Phasellus sollicitudin》, 《后果:Phasellus sollicitudin》, “……” ] 回报( <列表 header={<ListHeader header= "Properties List" />} 数据源={}列表 renderItem={(listItem, index) => <列表。项目键={index}> {listItem} </List. index . > {listtitem}项> } /> ) }
当声明React Class组件时,使用React。ComponentClass而不是React。组件,那么它将修复ts错误。
以下方法对我有用:https://github.com/microsoft/TypeScript/issues/28631#issuecomment-472606019我通过这样做来修复它:
const Component = (isFoo ? FooComponent : BarComponent) as React.ElementType