错误"JSX元素类型'...'没有任何构造或调用签名"是什么意思?意思是? [英] What does the error "JSX element type '...' does not have any construct or call signatures" mean?

查看:2867
本文介绍了错误"JSX元素类型'...'没有任何构造或调用签名"是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码:

function renderGreeting(Elem: React.Component<any, any>) {
    return <span>Hello, <Elem />!</span>;
}

我遇到错误:

JSX元素类型Elem没有任何构造或调用签名

JSX element type Elem does not have any construct or call signatures

这是什么意思?

推荐答案

这是构造函数实例之间的混淆.

请记住,当您在React中编写组件时:

Remember that when you write a component in React:

class Greeter extends React.Component<any, any> {
    render() {
        return <div>Hello, {this.props.whoToGreet}</div>;
    }
}

您以这种方式使用它:

return <Greeter whoToGreet='world' />;

不要以这种方式使用它:

let Greet = new Greeter();
return <Greet whoToGreet='world' />;

在第一个示例中,我们传递了Greeter(组件的构造函数).这是正确的用法.在第二个示例中,我们传递的是 instance .这是不正确的,并且将在运行时失败,并显示诸如对象不是函数"之类的错误.

In the first example, we're passing around Greeter, the constructor function for our component. That's the correct usage. In the second example, we're passing around an instance of Greeter. That's incorrect, and will fail at runtime with an error like "Object is not a function".

此代码存在问题

function renderGreeting(Elem: React.Component<any, any>) {
    return <span>Hello, <Elem />!</span>;
}

是因为它期望 instance .您想要一个为React.Component使用构造函数的函数:

is that it's expecting an instance of React.Component. What you want a function that takes a constructor for React.Component:

function renderGreeting(Elem: new() => React.Component<any, any>) {
    return <span>Hello, <Elem />!</span>;
}

或类似地:

function renderGreeting(Elem: typeof React.Component) {
    return <span>Hello, <Elem />!</span>;
}

这篇关于错误"JSX元素类型'...'没有任何构造或调用签名"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆