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
Fix Uncaught ReferenceError: useState is not defined in React
Publish Date:2025/03/15 Views:142 Category:React
-
When we use the useState hook in our code but forget to import it, it generates the error Uncaught ReferenceError: useState is not defined. To fix this error, you need to import the hook before using it import {useState} from react . // ?️
React error Uncaught ReferenceError: process is not defined solution
Publish Date:2025/03/15 Views:116 Category:React
-
To resolve the “Uncaught ReferenceError: process is not defined” error in React, open a terminal in the root directory of your project and update the version of the `react-scripts` package by running `npm install react-scripts@latest` and reinstal
How to solve the "Function components cannot have string refs" error in React
Publish Date:2025/03/15 Views:138 Category:React
-
When we use a string as a reference in a function component, we get the error "Function components cannot have string refs". To fix this error, we need to use the useRef() hook to get a mutable ref object that we can use as a component.
React Error Property 'X' does not exist on type 'Readonly<{}>'
Publish Date:2025/03/15 Views:139 Category:React
-
The React.js error “Property does not exist on type 'Readonly'” occurs when we try to access the props or state of an untyped class component. To fix the error, you need to use generics on the React.Component class to type the props or state of th
Solve React's Unexpected default export of anonymous function error
Publish Date:2025/03/15 Views:120 Category:React
-
When we try to export an anonymous function using default export, it results in the warning “Unexpected default export of anonymous function.” To fix this error, you need to give the function a name before exporting it.
React error Type '() => JSX.Element[]' is not assignable to type FunctionCompo
Publish Date:2025/03/15 Views:52 Category:React
-
When we try to return an array of elements from a function component, a React.js error “Type '() => JSX.Element[]' is not assignable to type FunctionComponent” occurs. To fix the error, you need to wrap the array of elements in a React fragment.
How to solve React Type {children: Element} has no properties in common with type
Publish Date:2025/03/15 Views:103 Category:React
-
React.js error "Type {children: Element} has no properties in common with type IntrinsicAttributes" occurs when we try to pass children prop to a component without any props. To resolve the error, you need to define and type the properties on the comp
Fix the value prop on input should not be null error in React
Publish Date:2025/03/15 Views:142 Category:React
-
The warning "value prop on input should not be null" is caused when we set the initial value of an input to null or override the initial value setting it to null, e.g. from an empty API response. Use a fallback value to solve this problem.
Property does not exist on type 'JSX.IntrinsicElements' error in React
Publish Date:2025/03/15 Views:189 Category:React
-
When the name of a component starts with a lowercase letter, an error "Property does not exist on type 'JSX.IntrinsicElements" will appear. To fix this error, you need to make sure you always start your component names with an uppercase letter, instal