Solve React error JSX element type does not have any construct or call signatures
When we try to pass an element or a React component as a parameter to another component but the input parameter is incorrect, we get the error "JSX element type does not have any construct or call signatures". To fix this error, we need to use React.ElementType
the type.
Below is sample code that produces the error.
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="jiyik.com" />
</div>
);
};
const App: React.FunctionComponent = () => {
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
return (
<div>
<Wrapper comp={heading} />
</div>
);
};
export default App;
We are trying to pass a React component as a prop to the Wrapper component but it is already typed JSX.Element
.
To fix this error, we have to type prop as React.ElementType
.
import React from 'react';
interface Props {
comp: React.ElementType; // 👈️ React.ElementType
}
const Wrapper: React.FunctionComponent<Props> = props => {
// 👇️ 组件名称必须以大写字母开头
const {comp: Comp} = props;
return (
<div>
<Comp name="jiyik.com" />
</div>
);
};
const App: React.FunctionComponent = () => {
// 👇️ 获取 name 参数
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
return (
<div>
<Wrapper comp={heading} />
</div>
);
};
export default App;
Note that you can React.ElementType
pass a generic type for the attribute type that the element expects.
In this example, we have to pass it an object with a name property of type string, since this is the property taken by the title component.
import React from 'react';
interface Props {
// ✅ 显式输入属性 name
comp: React.ElementType<{name: string}>;
}
const Wrapper: React.FunctionComponent<Props> = props => {
// 👇️ 组件名称必须以大写字母开头
const {comp: Comp} = props;
return (
<div>
<Comp name="jiyik.com" />
</div>
);
};
const App: React.FunctionComponent = () => {
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
return (
<div>
<Wrapper comp={heading} />
</div>
);
};
export default App;
Now we have explicitly typed the props that the comp element takes when it is used. This helps us take advantage of auto-completion in our IDE when passing properties to components.
We can also use React.ComponentType
but we need to input props.
import React from 'react';
interface Props {
// 👇️ 现在使用 React.ComponentType 👇️
comp: React.ComponentType<{name: string}>;
}
const Wrapper: React.FunctionComponent<Props> = props => {
// 👇️ 组件名称必须以大写字母开头
const {comp: Comp} = props;
return (
<div>
<Comp name="jiyik.com" />
</div>
);
};
const App: React.FunctionComponent = () => {
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
return (
<div>
<Wrapper comp={heading} />
</div>
);
};
export default App;
React.ComponentType
Generics in do not default to any
the type, so we need to explicitly type props.
Note that if we passed a JSX element as props to the component instead of an actual component, then would JSX.Element
be of the correct type.
import React from 'react';
interface Props {
// 👇️ 使用 JSX.Element 类型
comp: JSX.Element;
}
const Wrapper: React.FunctionComponent<Props> = props => {
const {comp: Comp} = props;
// 👇️ use as {Comp}
return <div>{Comp}</div>;
};
const App: React.FunctionComponent = () => {
const Heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
// 👇️ 我们传递了一个实际的 JSX 元素,因为我们没有将它作为 comp={Heading} 传递
return (
<div>
<Wrapper comp={<Heading name="jiyik.com" />} />
</div>
);
};
export default App;
The results are shown as
We type the comp property JSX.Element
as since we are passing an actual JSX element (not a component) to the Wrapper component.
We are passing a JSX element because we are passing the prop as instead comp={<Heading />}
of comp={(props) => <h2>Hello world</h2>}
.
注意
, in the first case, we passed a JSX element as a property, and in the second case, we passed a function that returns a JSX element (a functional component).
We should not try to use JSX elements as components in Wrapper components, e.g. don't do it <Comp />
, instead {Comp}
.
We didn't pass an actual component as a prop, we passed a JSX element, so it shouldn't be used as a component.
If none of the previous suggestions help, try updating your version of React typings by running the following command.
# 👇️ 使用 NPM
$ npm install react@latest react-dom@latest
$ npm install --save-dev @types/react@latest @types/react-dom@latest
# ----------------------------------------------
# 👇️ 使用 YARN
$ yarn add react@latest react-dom@latest
$ yarn add @types/react@latest @types/react-dom@latest --dev
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:188 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:187 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:102 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:172 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:153 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:60 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:
Solution to the error "does not contain a default export" in React
Publish Date:2025/03/16 Views:191 Category:React
-
When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.
Error in React: Attempted import error 'X' is not exported from Solution
Publish Date:2025/03/16 Views:78 Category:React
-
In React, the error “Attempted import error 'X' is not exported from” in React.js occurs when we try to import a named import that does not exist in the specified file. To fix the error, make sure the module has named exports and you have not obfu
Solve the Module not found: Can't resolve 'react-bootstrap' error
Publish Date:2025/03/16 Views:90 Category:React
-
To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap