ReasonJun

JSX.Element vs ReactNode vs ReactElement 본문

Frontend/React

JSX.Element vs ReactNode vs ReactElement

ReasonJun 2023. 7. 12. 16:04
728x90

JSX.Element, ReactNode, and ReactElement are all related to React, but they have different meanings and uses.

  • JSX.Element is a type that is used in TypeScript to represent a JSX expression. It is equivalent to the ReactElement type, but its props and type properties are both any. This means that JSX.Element can represent any kind of value, including ReactElements, strings, numbers, and objects.
  • ReactNode is a type that represents anything that React can render. This includes ReactElements, strings, numbers, objects, and even null and undefined. ReactNode is often used as the type for the children prop of a React component.
  • ReactElement is the type for elements in React. It is an object with three properties: type, props, and key. The type property specifies the type of the element, such as "div" or "button". The props property is an object that contains the properties of the element, such as its id, class, and style. The key property is an optional property that can be used to identify the element.

In general, you should use JSX.Element when you are using TypeScript and you need to represent a JSX expression. You should use ReactNode when you need to represent anything that React can render. And you should use ReactElement when you need to represent a specific type of React element.

 

// JSX.Element example
const jsxElement = <div>Hello, world</div>;

// ReactNode example
const reactNode = "Hello, world";

// ReactElement example
const reactElement = React.createElement("div", null, "Hello, world");

In the first example, jsxElement is a JSX.Element. This means that it is a type that is used in TypeScript to represent a JSX expression. It is equivalent to the ReactElement type, but its props and type properties are both any. This means that JSX.Element can represent any kind of value, including ReactElements, strings, numbers, and objects.

 

Here is a table that summarizes the differences between JSX.Element, ReactNode, and ReactElement:

 

Property JSX.Element ReactNode ReactElement
Type any any object
Props any any object
Key any undefined optional
Usage TypeScript general specific

https://stackoverflow.com/questions/58123398/when-to-use-jsx-element-vs-reactnode-vs-reactelement

 

When to use JSX.Element vs ReactNode vs ReactElement?

I am currently migrating a React application to TypeScript. So far, this works pretty well, but I have a problem with the return types of my render functions, specifically in my functional componen...

stackoverflow.com

https://www.totaltypescript.com/jsx-element-vs-react-reactnode

 

React.ReactNode vs JSX.Element vs React.ReactElement

Learn the differences between React.ReactNode and JSX.Element in TypeScript when working with React.

www.totaltypescript.com

 

728x90

'Frontend > React' 카테고리의 다른 글

React : context (state management)  (0) 2023.08.11
React.FC and JSX.Element  (0) 2023.07.12
React 18 : Transition  (0) 2023.06.16
React 18 : Suspense  (0) 2023.06.16
React 18 : Automatic batching  (0) 2023.06.16
Comments