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

先来一小段代码

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类型的范围更小。

相关推荐

  1. react组件元素类型总结

    2023-12-08 01:40:16       43 阅读
  2. React函数组件组件区别

    2023-12-08 01:40:16       10 阅读
  3. ReactReact懒加载组件lazySuspense

    2023-12-08 01:40:16       8 阅读
  4. TypeScript基础类型高级类型梳理总结

    2023-12-08 01:40:16       21 阅读
  5. react函数组件中Hooksahooks区别

    2023-12-08 01:40:16       44 阅读
  6. 一个展开收起业务组件(React)

    2023-12-08 01:40:16       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 01:40:16       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 01:40:16       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 01:40:16       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 01:40:16       18 阅读

热门阅读

  1. 大数据基础设施搭建 - Flume

    2023-12-08 01:40:16       25 阅读
  2. LeetCode40. Combination Sum II

    2023-12-08 01:40:16       34 阅读
  3. Docker

    Docker

    2023-12-08 01:40:16      26 阅读
  4. 绘制爆炸轨迹 III:绘制条形轨迹 Python

    2023-12-08 01:40:16       31 阅读
  5. 快速去除Word文档密码,全面解决你的困扰

    2023-12-08 01:40:16       36 阅读
  6. 介绍chatgpt原理及技术架构

    2023-12-08 01:40:16       41 阅读
  7. MySQL学习day04(一)

    2023-12-08 01:40:16       32 阅读
  8. qt反射基础

    2023-12-08 01:40:16       29 阅读
  9. android 13.0 framework禁用系统所有通知

    2023-12-08 01:40:16       37 阅读
  10. Linux下超轻量级Rust开发环境搭建:一、安装Rust

    2023-12-08 01:40:16       37 阅读
  11. python pandas dataframe常用数据处理总结

    2023-12-08 01:40:16       36 阅读