DEV Community

Cover image for 🧬 JSX at lowest level
Andrei L
Andrei L

Posted on • Updated on

🧬 JSX at lowest level

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                   */
}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Production createElement implementation
Development createElement 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
Development jsx implementation

Are you still here? Let's go deeper 😀

man hard working

So if everything is configured correctly, this example:

function Component() {
  return <div className="yay">lorem ipsum</div>
}
Enter fullscreen mode Exit fullscreen mode

Will be compiled to something like this:

import { jsx } from "react/jsx-runtime";
function Component() {
  return jsx("div", { className: "yay", children: "lorem ipsum" });
}
Enter fullscreen mode Exit fullscreen mode

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" });
}
Enter fullscreen mode Exit fullscreen mode

It will return:

function Component() {
  return {
    $$typeof: Symbol('react.element'),
    props: { className: 'yay', children: 'lorem ipsum' },
    type: "div",
  };
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

It will be compiled to this:

ReactDOM.render(
  { $$typeof: Symbol('react.element'), type: Component },
  rootElement
);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (12)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

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...

Spreading like cancer

Collapse
 
iamandrewluca profile image
Andrei L

😬

Collapse
 
ben profile image
Ben Halpern

Great post

Collapse
 
iamandrewluca profile image
Andrei L

Thanks!

Collapse
 
ps173 profile image
Pratham Sharma

Wow that's a great post right there

Collapse
 
iamandrewluca profile image
Andrei L

Thanks! Glad you liked it!

Collapse
 
zyabxwcd profile image
Akash

the person with the shovel gif is a funny insertion

Collapse
 
iamandrewluca profile image
Andrei L

😂 I always like to drop funny gifs mid air 😀

Collapse
 
calabiyauman profile image
Düg

I'm here, good post

Collapse
 
iamandrewluca profile image
Andrei L

Thanks!

Collapse
 
dudeastronaut profile image
Peter Perkins

Nice post - read the whole thing 👍 I liked how you broke down the JSX explanation using console.log 💯

Collapse
 
iamandrewluca profile image
Andrei L

Thanks! Glad you liked it!