<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kashif Ullah</title>
    <description>The latest articles on DEV Community by Kashif Ullah (@kashif_ullah_dev).</description>
    <link>https://dev.to/kashif_ullah_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1790617%2Fd4c58ef7-9bfe-413b-a9d0-fc4bbcd925b2.png</url>
      <title>DEV Community: Kashif Ullah</title>
      <link>https://dev.to/kashif_ullah_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kashif_ullah_dev"/>
    <language>en</language>
    <item>
      <title>How React JSX Gets Transformed Into JavaScript Behind the Scenes</title>
      <dc:creator>Kashif Ullah</dc:creator>
      <pubDate>Mon, 23 Sep 2024 09:45:20 +0000</pubDate>
      <link>https://dev.to/kashif_ullah_dev/how-react-jsx-gets-transformed-into-javascript-behind-the-scenes-467f</link>
      <guid>https://dev.to/kashif_ullah_dev/how-react-jsx-gets-transformed-into-javascript-behind-the-scenes-467f</guid>
      <description>&lt;p&gt;When you're writing React, you'll often see JSX – a syntax that looks like HTML within your JavaScript code. But have you ever wondered how this code runs in the browser?&lt;/p&gt;

&lt;p&gt;Here's the magic: JSX is not valid JavaScript! Browsers can’t understand it directly. Behind the scenes, a tool like Babel steps in to convert (or "transpile") JSX into regular JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Process:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1&lt;/strong&gt;.&lt;strong&gt;You write JSX&lt;/strong&gt; – It looks like HTML but lives within your React components. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2&lt;/strong&gt;.&lt;strong&gt;Babel transforms it&lt;/strong&gt; – During the build process, Babel converts the JSX into pure JavaScript using &lt;strong&gt;React.createElement&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = React.createElement('h1', null, 'Hello, World!');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3&lt;/strong&gt;.&lt;strong&gt;JavaScript in the browser&lt;/strong&gt; – Finally, this standard JavaScript is what the browser executes, building the DOM dynamically.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Babel work?
&lt;/h2&gt;

&lt;p&gt;Babel is a powerful JavaScript compiler that enables you to use the latest JavaScript features (including JSX in React) while ensuring compatibility across different browsers and environments. Here's a breakdown of how Babel works:&lt;br&gt;
&lt;strong&gt;1&lt;/strong&gt;.&lt;strong&gt;Parsing&lt;/strong&gt;&lt;br&gt;
Babel starts by parsing your modern JavaScript or JSX code into an &lt;strong&gt;Abstract Syntax Tree (AST).&lt;/strong&gt; The AST is a detailed tree-like structure that represents the code in a way that's easy for Babel (and other tools) to analyze and manipulate. During this step, Babel reads the code but doesn’t change it yet.&lt;br&gt;
For example, Babel takes this JSX code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And generates an AST, which represents the structure of this code.&lt;br&gt;
&lt;strong&gt;2&lt;/strong&gt;.&lt;strong&gt;Transforming&lt;/strong&gt;&lt;br&gt;
Next, Babel applies a series of &lt;strong&gt;plugins&lt;/strong&gt; to the &lt;strong&gt;AST&lt;/strong&gt;. Each plugin is responsible for transforming specific parts of the AST according to the &lt;strong&gt;rules&lt;/strong&gt; it defines. These transformations can include &lt;strong&gt;converting&lt;/strong&gt; modern JavaScript syntax (like arrow functions or async/await) into older, browser-compatible versions.&lt;br&gt;
For JSX, Babel transforms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = React.createElement('h1', null, 'Hello, World!');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3&lt;/strong&gt;. &lt;strong&gt;Code Generation&lt;/strong&gt;&lt;br&gt;
Once all transformations are done, Babel &lt;strong&gt;generates JavaScript code&lt;/strong&gt; from the transformed &lt;strong&gt;AST&lt;/strong&gt;. This is the final step where the human-readable output is produced, which the browser or Node.js can execute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets Conclude...&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why It Matters:
&lt;/h2&gt;

&lt;p&gt;JSX allows you to write more intuitive, component-based UI code, but it's Babel and the JavaScript runtime that bring it to life. Understanding this conversion helps you troubleshoot better and write more optimized React apps.&lt;/p&gt;

</description>
      <category>jsx</category>
      <category>javascript</category>
      <category>react</category>
      <category>babel</category>
    </item>
  </channel>
</rss>
