正文从这开始~

总览

当我们试图将元素或react组件作为属性传递给另一个组件,但是属性的类型声明错误时,会产生"JSX element type does not have any construct or call signatures"错误。为了解决该错误,可以使用React.ElementType类型。

这里有个例子来展示错误是如何发生的。

// App.tsx
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="James" />
</div>
);
}; const App: React.FunctionComponent = () => {
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>; return (
<div>
<Wrapper comp={heading} />
</div>
);
}; export default App;

我们尝试将一个React组件作为属性传递给Wrapper组件,但我们将该React组件的类型声明为JSX.Element

React.ElementType

为了解决该错误,将属性的类型声明为React.ElementType

// App.tsx
import React from 'react'; interface Props {
comp: React.ElementType; // ️ type it as React.ElementType
} const Wrapper: React.FunctionComponent<Props> = props => {
// ️ component names must start with capital letter
const {comp: Comp} = props;
return (
<div>
<Comp name="James" />
</div>
);
}; const App: React.FunctionComponent = () => {
// ️ takes a name prop
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>; return (
<div>
<Wrapper comp={heading} />
</div>
);
}; export default App;

请注意,React.ElementType可以为元素期望的属性类型传递一个泛型。

在这个例子中,我们必须传递给它一个具有字符串类型的name属性的对象,因为那是heading组件接收的属性。

// App.tsx
import React from 'react'; interface Props {
// explicitly type props comp takes
comp: React.ElementType<{name: string}>;
} const Wrapper: React.FunctionComponent<Props> = props => {
// ️ component names must start with capital letter
const {comp: Comp} = props;
return (
<div>
<Comp name="James" />
</div>
);
}; const App: React.FunctionComponent = () => {
const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>; return (
<div>
<Wrapper comp={heading} />
</div>
);
}; export default App;

现在我们显式地声明了元素在使用时所接受的comp属性的类型。这有助于我们在向组件传递属性时利用IDE的自动完成功能。

我们也可以使用React.ComponentType,但这样我们就需要对属性声明类型。

// App.tsx
import React from 'react'; interface Props {
// ️ now using React.ComponentType ️
comp: React.ComponentType<{name: string}>;
} const Wrapper: React.FunctionComponent<Props> = props => {
// ️ component names must start with capital letter
const {comp: Comp} = props;
return (
<div>
<Comp name="James" />
</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 中的泛型不能默认为any类型,因此我们需要显示地声明属性的类型。

传递JSX元素

如果你需要将JSX元素作为属性传递给组件,并且不是一个真正的组件,那么使用JSX.Element类型就是正确的。

// App.tsx
import React from 'react'; interface Props {
// ️ using JSX.Element type
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>; // ️ we are passing an actual JSX element
// because we didn't pass it as comp={Heading}
return (
<div>
<Wrapper comp={<Heading name="James" />} />
</div>
);
}; export default App;

我们将comp属性的类型声明为JSX.Element,因为我们传递了一个真正的JSX元素(不是组件)到Wrapper组件上。

我们传递了一个JSX元素,是因为我们将comp={<Heading />}作为属性进行传递,而不是comp={(props) => <h2>Hello world</h2>}

需要注意的是,在第一种情况下,我们传递的是一个JSX元素属性。而在第二种情况下,我们传递的是一个返回JSX元素的函数(一个功能组件)。

在Wrapper组件中,我们不应尝试使用JSX元素作为组件。比如说,不要这么写<Comp />,而要这么写{Comp}

我们没有传递一个真正的组件作为属性,我们传递的是一个JSX元素,所以它不应该作为一个组件使用。

更新类型包

如果前面的建议都没有帮助,试着通过运行以下命令来更新你的React类型的版本。

# ️ with NPM
npm install react@latest react-dom@latest npm install --save-dev @types/react@latest @types/react-dom@latest # ---------------------------------------------- # ️ with YARN
yarn add react@latest react-dom@latest yarn add @types/react@latest @types/react-dom@latest --dev

React报错之JSX element type does not have any construct or call signatures的更多相关文章

  1. React报错之组件不能作为JSX组件使用

    正文从这开始~ 总览 组件不能作为JSX组件使用,出现该错误有多个原因: 返回JSX元素数组,而不是单个元素. 从组件中返回JSX元素或者null以外的任何值. 使用过时的React类型声明. 返回单 ...

  2. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  3. go语言byte类型报错cannot use "c" (type string) as type byte in assignment

    练习Go修改字符串的时候遇到这个问题:cannot use "c" (type string) as type byte in assignment,代码如下: package m ...

  4. 报错: Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  5. Linux mount挂载磁盘报错 mount: wrong fs type, bad option, bad superblock on /dev/vdb

    Linux mount挂载磁盘报错  mount: wrong fs type, bad option, bad superblock on /dev/vdb Linux挂载磁盘报如下错误: moun ...

  6. React报错之Type '() => JSX.Element[]' is not assignable to type FunctionComponent

    正文从这开始~ 总览 当我们尝试从函数组件中返回元素组成的数组时,会产生"Type '() => JSX.Element[]' is not assignable to type Fu ...

  7. React报错之Property 'X' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 在React中,当我们试图访问类型为HTMLElement 的元素上不存在的属性时,就会发生Property 'X' does not exist on type 'HTMLEl ...

  8. React报错之Property 'value' does not exist on type 'HTMLElement'

    正文从这开始~ 总览 当我们试图访问一个类型为HTMLElement的元素上的value属性时,会产生"Property 'value' does not exist on type 'HT ...

  9. React报错之Cannot find name

    正文从这开始~ .tsx扩展名 为了在React TypeScript中解决Cannot find name报错,我们需要在使用JSX文件时使用.tsx扩展名,在你的tsconfig.json文件中把 ...

随机推荐

  1. JS倒计时(刷新页面不影响)的实现思路

    最近在做一个项目,用到了点击按钮实现倒计时,这个用js来实现很简单.但是遇到了一个问题 页面刷新后js重新加载导致 倒计时重新开始,或者直接初始化了 后来通过 cookie 保存来实现了js倒计时,关 ...

  2. A* K短路

    注:\(A*\) 求解K短路效率极其低下,时间复杂度\(O(nklog\ n)\),空间视题目而定,因为本质是爆搜,可求解数据范围较小的题目. 我们使用\(A*\)求解k短路: 首先需要预处理出估价函 ...

  3. 论文解读(LA-GNN)《Local Augmentation for Graph Neural Networks》

    论文信息 论文标题:Local Augmentation for Graph Neural Networks论文作者:Songtao Liu, Hanze Dong, Lanqing Li, Ting ...

  4. GitHub 简介

    用详细的图文对GitHub进行简单的介绍. git是一个版本控制工具,github是一个用git做版本控制的项目托管平台. 主页介绍: overview:总览.相当于个人主页. repositorie ...

  5. 《Unix 网络编程》15:Unix 域协议

    Unix 域协议 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 本 ...

  6. (1)《QT+OpenGL学习之我见》初始化窗口及三个重要函数 vs+Qt

    本章前言:本章讲如何利用VS和QT来创建一个基本的QOpenGLWidget窗口和有关联的三个核心函数,因为版本更新可能会有大同小异,但基本的不会有变换,有了QT的帮助,我们不需要下载opengL.g ...

  7. Python爬虫常用:谷歌浏览器驱动——Chromedriver 插件安装教程

    我们在做爬虫的时候经常要使用谷歌浏览器驱动,今天分享下这个Chromedriver 插件的安装方法. 第一步:打开谷歌浏览器打开设置面板 嫌枯燥的小伙伴可以点击此处找管理员小姐姐领取免费资料 第二步: ...

  8. TypeScript ReadonlyArray(只读数组类型) 详细介绍

    1.ReadonlyArray 简介 在TypeScript中,除了Array<T>类型,还有一个ReadonlyArray<T>类型,ReadonlyArray类型和Arra ...

  9. Elasticsearch面试题

    Elasticsearch面试题 1.Elasticsearch是如何实现master选举的? 1.对所有可以成为master的节点根据nodeId排序,每次选举每个节点都把自己所知道节点排一次序,然 ...

  10. [零基础学IoT Pwn] 复现Netgear WNAP320 RCE

    [零基础学IoT Pwn] 复现Netgear WNAP320 RCE 0x00 前言: 这是[零基础学IoT Pwn]的第二篇,上篇我们搭好了仿真环境后,模拟运行了Netgear WNAP320固件 ...