𧬠JSX at lowest level
Andrei L
Posted on October 7, 2021
this post is a precursor for upcoming JSX posts
So we all heard that JSX is a specific JavaScript syntax that is used by React to render components. Well, I would say JSX is just HTML in JavaScript. Or Java-Script-Xml π because HTML is a sibling of XML and children of SGML. Or maybe is just Java-Script e-X-tended.
When people ask me what is React? I tell them that React is just JavaScript functions that return HTML. And this is basically a template engine.
function Component() {
/* ^^^^^^^^^^^^^^^^ */
/* ^^^ JavaScript */
return <div className="yay">lorem ipsum</div>
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^ HTML */
}
Draft: JSX Specification
XML-LIKE SYNTAX EXTENSION TO ECMASCRIPT
http://facebook.github.io/jsx/
Ok, but HTML syntax is allowed only in the DOM, browser JavaScript does not support it. So it means we need to compile that to something that is supported by the browser.
JSX is just some syntactic sugar that is compiled to valid JavaScript.
What is JSX Pragma?
https://www.gatsbyjs.com/blog/2019-08-02-what-is-jsx-pragma/
https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#pragma
At the moment it compiles to something like this.
function Component() {
return React.createElement("div", { className: "yay" }, "lorem ipsum");
}
As you can see the <div>
syntax sugar is compiled to React.createElement
. That's why we need to have React imported at the top of the file. Otherwise we will get an runtime error that React cannot be found.
import React from 'react'
Production
createElement
implementation
DevelopmentcreateElement
implementation
But that was until React v17.0, Babel v7.9 and TypeScript v4.1, because after that they decided to extract JSX creation in a separate factory detached from React, and it is imported automatically π π π
Production
jsx
implementation
Developmentjsx
implementation
Are you still here? Let's go deeper π
So if everything is configured correctly, this example:
function Component() {
return <div className="yay">lorem ipsum</div>
}
Will be compiled to something like this:
import { jsx } from "react/jsx-runtime";
function Component() {
return jsx("div", { className: "yay", children: "lorem ipsum" });
}
In a future stable release (already), React will support a group of new functions for instantiating JSX elements as an alternative to the legacy general-purpose React.createElement function. This will allow optimizing them better in the future.
So we see that <div>
syntax sugar is compiled to jsx(...)
at build time, but what happens with this call at runtime? Well, let's console.log this call.
When React calls Component
function Component() {
return jsx("div", { className: "yay", children: "lorem ipsum" });
}
It will return:
function Component() {
return {
$$typeof: Symbol('react.element'),
props: { className: 'yay', children: 'lorem ipsum' },
type: "div",
};
}
Actually there is an open RFC that it seems that in the end React team will decide to compile directly JSX down to this object that is returned.
And for example if we want to render our Compnent
to the DOM. Having this call
ReactDOM.render(<Component />, rootElement);
It will be compiled to this:
ReactDOM.render(
{ $$typeof: Symbol('react.element'), type: Component },
rootElement
);
In the end our components are just some functions that return some objects that represent what we want rendered. So when we create our components, what we return is not what is rendered. ReactDOM calls our function components with some props, see this object definition that is returned and decides what and how to render on the screen. And JSX? JSX is just some syntax that is familiar to us because we know HTML.
But now we know that till our component goes down to ReactDOM it passes through some steps and transformations.
Also JSX is no more a React only thing, it's already used in a lot of projects, like vue, stenciljs, dom-chef, and many others...
Have a feeling that noone is reading these articles π
Thanks reader if you are here! β₯οΈ
Cover Photo by Alexandre Debiève on Unsplash
Posted on October 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
January 4, 2023