react的组件和元素的类型总结

news/2024/10/22 9:16:01/

先来一小段代码

const Demo = <div>Demo</div>const App = () => {return (<div><Demo></Demo></div>);
}

不知道这段代码大家会不会发现是错误的,这里的Demo 是一个JSX,并不是一个组件,所有不能使用<Demo />这种方式内嵌到其他组件中,正确的方式应该是

const Demo = <div>Demo</div>const App = () => {return (<div>{ Demo }</div>);
}

再来看一个例子

const Demo = ({children}) => <div>{children}</div>const App = () => {return (<div><Demo>Demo</Demo></div>);
}

这里的const Demo = ({children}) => <div>{children}</div>中的children也是渲染一个JSX,所以使用花括号的方式渲染,也就是{children}

总结一下

如果是JSX,使用花括号的方式渲染
如果是组件,使用尖括号包裹渲染

我们再来看一下组件类型。

其实React中描述组件类型有很多方式,刚开始的时候还是很容易迷惑的。例如:

import React, { ComponentType, FunctionComponent } from "react"const App:React.FC<any> = () => {return (<div>Demo</div>);
}const App:ComponentType<any> = () => {return (<div>Demo</div>);
}const App:FunctionComponent<any> = () => {return (<div>Demo</div>);
}const App:() => JSX.Element = () => {return (<div>Demo</div>);
}

方面这几种定义组件的方式都不会报错。并且都是正确的方式,我们平时大概都用React.FC

其实这几种方式都有很强的关系,不是超集,就是子集。我们把他们的定义找到,来看一下:

React.FC

type FC<P = {}> = FunctionComponent<P>;

ComponentType

 type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;

FunctionComponent

  interface FunctionComponent<P = {}> {(props: P, context?: any): ReactNode;propTypes?: WeakValidationMap<P> | undefined;contextTypes?: ValidationMap<any> | undefined;defaultProps?: Partial<P> | undefined;displayName?: string | undefined;}

JSX

declare global {/*** @deprecated Use `React.JSX` instead of the global `JSX` namespace.*/namespace JSX {interface Element extends React.ReactElement<any, any> {}// .....// 这里省略一些其他类型
}interface ReactElement<P = any,T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>,> {type: T;props: P;key: string | null;}

ReactNode

     type ReactNode =| ReactElement| string| number| Iterable<ReactNode>| ReactPortal| boolean| null| undefined| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES];

这里可以发现React.FC其实就是FunctionComponentComponentTypeComponentClassFunctionComponent的集合,而FunctionComponent的函数表达类型是 (props: P, context?: any): ReactNode;,然后ReactNode是一个联合类型,其中的一个类型就是ReactElement

分析到这里,我们再来看一下JSX.ElementJSX.Element继承至React.ReactElement

所以到这里,这4个类型都有所关联,这里有一些绕。这里只是想说,如果以后看到这些类型,希望能不会那么困惑。

还有一个小注意点,类似于vue的插槽吧,来看一个demo

import { ComponentType, FunctionComponent, ReactNode } from "react"type DemoProps = {children: ReactNode 
}const Demo: React.FC<DemoProps> = ({children}) => <div>{children}</div>const App = () => {return (<div><Demo>Demo</Demo></div>);
}

这里我们声明组件Demochildren类型是ReactNode,或者你定义成JSX.Element类型也可以,不过需要注意的是JSX.Element类型的范围更小。


http://www.ppmy.cn/news/1230132.html

相关文章

【开源】基于JAVA的超市自助付款系统

项目编号&#xff1a; S 008 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S008&#xff0c;文末获取源码。} 项目编号&#xff1a;S008&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、研究内容2.1 商品类型模块2.2 商品模块2.3 超市账…

「Verilog学习笔记」实现3-8译码器①

专栏前言 本专栏的内容主要是记录本人学习Verilog过程中的一些知识点&#xff0c;刷题网站用的是牛客网 分析 ① 本题要求根据38译码器的功能表实现该电路&#xff0c;同时要求采用基础逻辑门实现&#xff0c;那么就需要将功能表转换为逻辑表达式。 timescale 1ns/1nsmodule d…

elementui表格自定义指令控制显示哪些列可以拖动

Vue.directive(tableBorder, function (el, {value}) {// value允许传字符串数字和数组el.classList.add(z_table_hasBorder)let hasStyle el.querySelector(style)if(hasStyle){hasStyle.remove()}let style document.createElement(style)let str .z_table_hasBorder .el…

见面礼——图论

给定一个 n 个点 n 条边的无向图&#xff0c;你需要求有多少种选择图上的一个点 p 和一条边 (x,y) 的方案&#xff0c;使得删去 (x,y) 后图变成一棵树&#xff0c;且这棵树以 p 为根时每个节点的儿子个数均不超过 3。保证至少存在一种这样的方案。 Input 输入的第一行一个整数…

微信小程序内嵌h5页面,实现动态设置顶部标题的功能

一、需求描述 使用HBuilder X作为开发工具&#xff0c;vue作为开发语言&#xff0c;开发微信小程序。微信小程序页面内嵌h5页面&#xff0c;即<web-view></web-view>标签。通过设置不同url连接地址&#xff0c;设置不同的标题。 二、失败做法 页面A嵌入h5页面&a…

JSP页面文本展示正常 但定义在java代码中的内容 输出在页面上会变成问号 问题解决

这里 我直接写在界面上的内容就是正常的 但是 java代码中定义的内容 就会变成问号 造成这个情况的原因可能是多样的 首先要确保JDK没问题 然后是 页面顶部配置 <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-…

【Python】给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200

2、问题描述 给定一个长度为n的数列&#xff0c;将这个数列按从小到大的顺序排列。1<n<200 样例输入 5 8 3 6 4 9 样例输出 3 4 6 8 9 n int(input()) a list(map(int,input().split())) a.sort() for i in a:print(i,end ) 运行结果&#xff1a;

Web 自动化神器 TestCafe(二)—元素定位篇

今天主要给大家介绍一下testcafe这个框架元素定位的方法。 一、CSS 选择器定位 使用 testcafe 对元素进行操作的时候&#xff0c;我们可以直接通过 CSS 选择器指定要操作的元素&#xff0c;比如&#xff0c;点击元素&#xff0c;input 输入文本内容&#xff0c;如下&#xff1…