Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript React.ReactElement vs JSX.Element

Basic Question:

What is the difference between the types React.ReactElement vs JSX.Element? Are they the same, when should I use each one?

like image 428
jaimefps Avatar asked May 25 '19 21:05

jaimefps


People also ask

What is the difference between JSX element ReactNode and ReactElement?

ReactElement is the type for elements in React, either created via JSX or React. createElement. ReactNode is wider, it can be text, number, boolean, null, undefined, a portal, a ReactElement, or an array of ReactNodes. It represents anything that React can render.

What is JSX element TypeScript?

JSX is an embeddable XML-like syntax. It is meant to be transformed into valid JavaScript, though the semantics of that transformation are implementation-specific.

What is JSX element?

What is JSX? JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.

What is ReactNode TypeScript?

The ReactFragment , which is included in the ReactNode type, includes an empty interface. Due to the way that TypeScript handles excess property checks, this means that the ReactNode type will accept any object except an object literal. For almost all intents and purposes, it is functionally equivalent to an any type.


1 Answers

Even though JSX can be used in React to create React elements, React does not require JSX to work, you could replace it with vanilla JavaScript.

React elements are simply defined as a description of what you want to see on the screen. In other words, React elements are the instructions for how the browser DOM should be created.

A ReactElement is an object with a type and props. React reads these objects and uses them to construct the DOM. JSX.Element, on the other hand, is a ReactElement with generic type for props and type being any.

like image 113
Bárbara Peres Avatar answered Sep 21 '22 17:09

Bárbara Peres