首页 > 其他分享 >JSX.Element 和 React.ElementType的区别是什么?

JSX.Element 和 React.ElementType的区别是什么?

时间:2024-04-09 09:11:06浏览次数:16  
标签:Element React 类型 组件 ElementType JSX

在React和TypeScript中,JSX.ElementReact.ElementType代表了两种不同的概念:

  1. JSX.Element
    JSX.Element 是一个类型,表示由JSX编译后生成的实际React元素对象。
    当你在React应用中使用JSX编写组件时,每一个JSX表达式都会编译为一个JSX.Element对象。例如:

    const MyComponent: React.FC = () => <div>Hello World</div>;
    const myElement: JSX.Element = <MyComponent />; // 此处的myElement就是一个JSX.Element类型
    

    这个类型代表着React虚拟DOM树中的一个节点,它是React组件在渲染时生成的真实DOM节点的抽象表示。

  2. React.ElementType

    React.ElementType 是一个类型别名,它定义了可以用作React组件的元素类型的类型。它可以是以下任一类型:

    • 一个React组件类(如class ComponentClass extends React.Component
    • 一个函数组件(如const FunctionalComponent: React.FC
    • 一个原生DOM元素的标签名字符串(如 'div''span'
    type ElementType<P = any> = React.ComponentType<P> | string;
    

    这个类型常用于函数组件的props类型定义中,尤其是children属性或者其他接受React元素作为值的props。例如:

    interface MyProps {
      children: React.ReactNode | React.ElementType;
    }
    
    const MyContainer: React.FC<MyProps> = ({ children }) => (
      <div>{children}</div>
    );
    
    // 正确的用法
    <MyContainer>
      <div>Hello World</div>
      {'Or just text'}
      <AnotherComponent />
    </MyContainer>
    
    // 或者传递一个组件类型
    <MyContainer children={<AnotherComponent />} />
    

总结来说,JSX.Element 是由JSX编译后生成的具体React元素对象,而 React.ElementType 是指可以被用来创建React元素的类型,它可以是一个组件类型或者是原生HTML标签名。

标签:Element,React,类型,组件,ElementType,JSX
From: https://www.cnblogs.com/longmo666/p/18123086

相关文章

  • react路由使用
    在介绍ReactRouter的概念以前,需要先区分两个概念:react-router:为React应用提供了路由的核心功能;react-router-dom:基于react-router,加入了在浏览器运行环境下的一些功能。1.安装(本文6.22.3版本)npmi react-router-dom -S2.创建router/index.jsimportGoo......
  • react ref和context
    ref获取domcontext类似provider和injected,用于嵌套很深的爷孙组件传值子组件使用父组件创建的context对象,不能自己创建context创建在函数组件和class组件都是一样的exportletContext1=React.createContext('')<Context1.Providervalue='Contextvalue函数组件'>......
  • react 函数组件和hook
    函数组件1.函数组件没有生命周期2.函数组件没有this3.函数组件通过hook完成各种操作4.函数组件本身就是render函数5.props在函数第一个参数解释useState参考https://www.cnblogs.com/ssszjh/p/14581014.htmlprops参考https://www.cnblogs.com/ssszjh/p/18118746生命周期......
  • react 性能问题和优化
    某个组件更新,子组件也会一起更新react更新采用时间切片,vue则是依赖收集执行更新操作为16ms,如果操过16ms,先暂停更新,让浏览器先渲染时间切片时间是16ms,因为人眼刷新率约60帧,60hz为16ms1.避免state改为同样的值(class用PureComponent,函数组件默认已经处理了)2.处理子组......
  • react 生命周期
    1.class组件初次挂载1.constructor2.getDerivedStateFromProps3.render4.componentDidMount 更新数据1.getDerivedStateFromProps2.shouldComponentUpdate3.render4.componentDidUpdate备注:1.PureComponent里不能写shouldComponentUpdate2.优化性能一般在shoul......
  • react中redux基本使用二
    1.action传参,用payload属性接收  <buttononClick={()=>dispatch(addNum(2))}>+2</button> 2.redux中异步操作,与同步类似,需要比同步多封装一个函数//使用RTK创建store,createSlice创建reducer的切片//使用RTK创建store,createSlice创建reducer的切片import......
  • react中redux基本使用
    1.安装 npminstall@reduxjs/toolkitreact-redux2.创建store目录 3.创建counterStore.js//使用RTK创建store,createSlice创建reducer的切片import{createSlice}from"@reduxjs/toolkit";constcounterSlice=createSlice({//需要一个配置对象作为参数,通过......
  • [React] Using key prop to reset component to avoid useEffect hook
    ThecomponentusinguseEffectwhichisnotnecessary:functionTopicEditor({selectedTopicId}){const[enteredNote,setEnteredNote]=useState('');constselectedTopic=DUMMY_TOPICS.find(topic=>topic.id===selectedTopicId)......
  • 全栈的自我修养 ———— react路由两种传参方法
    react路由传参1、传统传参跳转前:跳转后:结果:2、配置传参跳转前:配置routes:跳转后:结果:1、传统传参跳转前:import{useNavigate}from"react-router-dom";<divclassName='login'onClick={()=>navigator('/public/login?id=1')}> <div......
  • 全栈的自我修养 ———— react router6默认二级路由配置?嵌套时候如何实现默认导航
    在组件嵌套时候小编定义了一个共同组件于/public地址下,小编发现如果直接访问public是只有外部组件的页面,小编目标是访问public时候直接访问index页面,小编找了很多资料最终自己使出来了一个办法如下!!小编自己发现的后来查找到的小编自己发现的即把{pat......