![]() |
VOOZH | about |
React has revolutionized UI development by introducing JSX, a syntax that blends JavaScript with HTML-like elements. While JSX simplifies the process of building components, understanding its inner workings is key to creating scalable and performant applications. This article explores how inline JavaScript within JSX is executed, the transformation process from JSX to JavaScript, and the tools used in this conversion, offering insights into the mechanics behind React's modern UI development approach.
These are the following topics that we are going to discuss:
Table of Content
JavaScript XML or JSX is known to be an add-up in the grammar of markup languages that contain JavaScript. In many instances, JS cannot perform certain operations such as designing a website’s layout. This is why this very helpful technology is derived it enables a lot of these things to be done in JavaScript code and JavaScript code alone without the bulk of unnecessary voices.
Example:
const Greeting = () => {
return <h1>Hello, World!</h1>;
};
Under the hood, browsers don't understand JSX. Therefore, JSX must be transformed into standard JavaScript before it can be executed. This transformation process is compulsory for the seamless integration of JSX within React applications.
Before directly going into the transformation process, it's essential to understand the rationale behind JSX's creation and its benefits.
Example: Conditional Rendering in JSX
const UserGreeting = ({ isLoggedIn }) => {
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
</div>
);
};
Babel is a JavaScript compiler widely used in the React ecosystem to transform JSX into standard JavaScript. It serves as the important ladder of the JSX transformation process, enabling developers to write modern JavaScript and JSX without worrying about browser compatibility.
Babel is a very famous transpiler that basically allows us to use future JavaScript in today’s browsers. In simple words, it can convert the latest version of JavaScript code into the one that the browser understands. Transpiler is a tool that is used to convert source code into another source code that is of the same level. The latest standard version that JavaScript follows is ES2020 which is not fully supported by all browsers hence we make use of a tool such as ‘babel’ so that we can convert it into the code that today’s browser understands.
Babel utilizes plugins to handle specific transformations. For JSX, the primary plugin is @babel/plugin-transform-react-jsx, which is responsible for converting JSX syntax into React.createElement calls.
Configuration Example:
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-transform-react-jsx"]
}
Understanding the step-by-step process of how JSX is transformed into JavaScript provides valuable insights into React's inner workings. Let's explore each stage in detail.
This is the first step in parsing, it is where Babel scans through the JSX code in a bid to understand it is structured and what it actually means.
Example: Given the following JSX code
const element = <h1 className="greeting">Hello, World!</h1>;After parsing and understanding a particular piece of code, Babel creates an AST, which is a structure that defines the relationship between different parts of the code in tree structures.
Visualization:
With the AST in place, Babel transforms the JSX elements into standard JavaScript function calls.
const element = <h1 className="greeting">Hello, World!</h1>;Transformed JavaScript:
const element = React.createElement(
'h1',
{ className: 'greeting' },
'Hello, World!'
);
The final stage involves generating executable JavaScript code from the transformed AST.
Final Output:
const element = React.createElement(
'h1',
{ className: 'greeting' },
'Hello, World!'
);
The transformed code, though functionally equivalent to the original JSX, operates differently under the hood. Let's do in detail to the React.createElement function to understand how it works.
React.createElement is a fundamental function in React that creates a React element, which is a lightweight description of what should appear on the screen.
Syntax:
React.createElement(
type,
[props],
[...children]
);
Example:
const element = React.createElement(
'button',
{ onClick: handleClick },
'Click Me'
);
Equivalent JSX:
const element = <button onClick={handleClick}>Click Me</button>;While Babel efficiently transforms JSX into JavaScript, several optimizations enhance performance and maintainability.
In earlier versions of React, developers needed to import React to use JSX because React.createElement was called explicitly. However, with the introduction of the automatic JSX runtime in React 17, Babel can automatically handle JSX transformations without the need to import React explicitly.
Configuration Example:
{
"presets": [
["@babel/preset-react", {
"runtime": "automatic"
}]
]
}
Optimizing component re-renders is crucial for performance. Using React.memo or Pure Components ensures that components only re-render when necessary.
Example with React.memo:
const Greeting = React.memo(({ name }) => {
return <h1>Hello, {name}!</h1>;
});
Dividing the application into smaller chunks and loading them on demand reduces initial load times.
Example:
const OtherComponent = React.lazy(() => import('./OtherComponent'));
function MyComponent() {
return (
<Suspense fallback={<div>Loading...</div>}>
<OtherComponent />
</Suspense>
);
}
Babel offers various plugins that can optimize the transformed code, such as minification, dead code elimination, and more.
Example: Using babel-preset-minify:
{
"presets": ["@babel/preset-env", "@babel/preset-react", "minify"]
}
Understanding common pitfalls when working with JSX can save time and prevent errors.
Every JSX tag must be properly closed. Forgetting to close tags can lead to compilation errors.
Incorrect:
const element = <img src="image.png">;Correct:
const element = <img src="image.png" />;Since class is a reserved keyword in JavaScript, JSX uses className to assign CSS classes.
Incorrect:
const element = <div class="container"></div>;Correct:
const element = <div className="container"></div>;JSX allows embedding JavaScript expressions within curly braces {}. Forgetting them can lead to unexpected behavior.
Incorrect:
const element = <h1>Hello, name!</h1>;Correct:
const element = <h1>Hello, {name}!</h1>;A React component must return a single parent element. Wrapping multiple elements within a parent like <div> or using React fragments is necessary.
Incorrect:
const MyComponent = () => {
return (
<h1>Hello</h1>
<p>World</p>
);
};
Correct:
const MyComponent = () => {
return (
<>
<h1>Hello</h1>
<p>World</p>
</>
);
};
Must Read:
JSX is a very effective way of writing React components because it helps the developers to incorporate familiar HTML within JavaScript. Understanding this flow of conversion is imperative for the optimization of the applications built using React and the rectification of possible problems that may arise.
Babel is at the center of this transformation process, which begins from the parse of JSX to the creation of AST to the call of React.createElement and finally, the executable code in JavaScript. With even more increasing improvement and optimization, still JSX is used in the React platform to increase efficiency of the developers and the performance of the applications.
If one is able to comprehend the transformation of JSX, is in possession of the best practices and is certified against the common mistakes that developers make, there is off no doubt that the possibility of developing effective, manageable and fast React applications will be achieved.