When to use JSX.Element vs ReactNode vs ReactElement?如何在ts中选择jsx.element和ReatNode类型

A ReactElement is an object with a type and props.

interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
   type: T;
   props: P;
   key: Key | null;
}

A ReactNode is a ReactElement, a ReactFragment, a string, a number or an array of ReactNodes, or null, or undefined, or a boolean:

//从这里能看出,ReactNode是ReactElement的父级,同时ReactNode是最大类型包括string等。所以当值为ReactFragment等时,优先使用ReactNode

type ReactText = string | number;
type ReactChild = ReactElement | ReactText;

interface ReactNodeArray extends Array<ReactNode> {}
type ReactFragment = {} | ReactNodeArray;
type ReactNode = ReactChild | ReactFragment | ReactPortal | boolean | null | undefined;

JSX.Element is a ReactElement, with the generic type for props and type being any. It exists, as various libraries can implement JSX in their own way, therefore JSX is a global namespace that then gets set by the library, React sets it like this:

declare global {
  namespace JSX {
    interface Element extends React.ReactElement<any, any> { }
  }
}

 <p> // <- ReactElement = JSX.Element
   <Custom> // <- ReactElement = JSX.Element
     {true && "test"} // <- ReactNode
  </Custom>
 </p>

Why do the render methods of class components return ReactNode, but function components return ReactElement?

Indeed, they do return different things. Components return:

 render(): ReactNode;

And functions are “stateless components”:

 interface StatelessComponent<P = {}> {
    (props: P & { children?: ReactNode }, context?: any): ReactElement | null;
    // ... doesn't matter
}

As you can see, functional components can have ReactNodes as children, but they must return a ReactElement or null. If you think about it, it is actually the same as with Components: They are itself ReactElements, but they can have ReactNodes as children.

How do I solve this with respect to null?

Type it as ReactElement | null just as react does. Or let Typescript infer the type.

在React生态系统中,`React.Element`, `React.FC`, `JSX.Element`, `React.ReactNode` 都与组件的创建处理有关,它们之间有一些关键的区别: 1. **React.Element**: 这是一个泛型类型,代表了一个React元素,可以是函数组件、类组件、或者通过JSX直接创建的元素。例如: ```jsx const element = <div>Hello World</div>; ``` `element`就是一个React.Element实例。 2. **React.FC (Functional Component)**: 是一个简写的语法糖,用于表示纯函数组件。这种类型的组件没有状态,只接受props作为输入并返回React Element。如: ```jsx type MyComponentProps = { name: string }; function MyComponent(props: MyComponentProps) { return <h1>Hello, {props.name}!</h1>; } ``` `MyComponent`就是一个React.FC。 3. **JSX.Element**: JSX是一种JavaScript库,它扩展了JavaScript语言,使得我们可以编写类似于HTML的标记结构。当你在JavaScript代码中看到类似 `<div>` 的标签时,实际上是生成了一个`JSX.Element`实例。 4. **React.ReactNode**: 这是一个更为通用的类型,它可以包含所有React可以处理的元素,包括元素、null、字符串、甚至其他节点(如自定义组件)。比如: ```jsx const node: React.ReactNode = <MyComponent /> || 'Hello from text'; ``` `node`既可以是组件,也可以是文本或其他内容。 总结来说,`React.Element`是React组件的一种实例形式,而`React.FC`是一种特定类型的组件,`JSX.Element`是JSX表达式产生的结果,而`React.ReactNode`则更为宽泛,包含了React可以处理的所有内容。在编写代码时,可以根据需要选择使用哪种类型
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值