I wrote some code:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
I'm getting an error:
JSX element type
Elem
does not have any construct or call signatures
What does it mean?
reactjstypescript
I wrote some code:
function renderGreeting(Elem: React.Component<any, any>) {
return <span>Hello, <Elem />!</span>;
}
I'm getting an error:
JSX element type
Elem
does not have any construct or call signatures
What does it mean?
Best Answer
This is a confusion between constructors and instances.
Remember that when you write a component in React:
You use it this way:
You don't use it this way:
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 ofGreeter
. That's incorrect, and will fail at runtime with an error like "Object is not a function".The problem with this code
is that it's expecting an instance of
React.Component
. What you want is a function that takes a constructor forReact.Component
:or similarly: