Programming & Development / April 14, 2025

Using ReactDOM with Babel: JSX Transpilation and DOM Rendering

ReactDOM Babel JSX transpilation React JSX Babel presets JavaScript ES6 browser compatibility React setup

πŸ”§ What is Babel in the React Ecosystem?

Babel is a JavaScript compiler that transforms modern JavaScript (ES6+) and JSX syntax into code that browsers can understand. When you're working with React and ReactDOM, Babel plays a crucial role in converting JSX into valid JavaScript.

🧱 Why Babel is Important for React

React uses JSX, a syntax extension that allows you to write HTML-like code inside JavaScript. Browsers can’t interpret JSX directly β€” that’s where Babel comes in.

βœ… JSX Example (Before Babel):

jsx

const element = <h1>Hello, world!</h1>;

❌ What the browser understands (After Babel):

js

const element = React.createElement('h1', null, 'Hello, world!');

πŸ› οΈ Setting Up Babel with React and ReactDOM

When creating a simple React app without a build tool like Create React App (CRA), you can manually set up Babel like this:

βœ… HTML Template:

html

<!DOCTYPE html>
<html>
  <head>
    <title>React + Babel</title>
    <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>

    <!-- JSX with Babel -->
    <script type="text/babel">
      const App = () => <h1>Hello from React and Babel!</h1>;
      ReactDOM.createRoot(document.getElementById('root')).render(<App />);
    </script>
  </body>
</html>

πŸ” Notes:

  • The type="text/babel" attribute tells the browser to use Babel to transpile the JSX.
  • ReactDOM.createRoot(...).render(...) is the modern way of rendering in React 18+.

βš™οΈ Behind the Scenes: Babel’s Role

  1. You write JSX in React code.
  2. Babel transpiles JSX into React.createElement() calls.
  3. ReactDOM takes those calls and renders them into the actual browser DOM.

πŸ“¦ Using Babel with a Build Tool (Advanced Setup)

In a professional React project, Babel is usually integrated via Webpack, Vite, or Parcel.

Example babel.config.json:

json

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

These presets:

  • @babel/preset-env – Transpiles modern JS (ES6+)
  • @babel/preset-react – Transpiles JSX into JS

πŸ“Œ Summary

  • Babel is essential for using JSX in React apps.
  • It transforms your modern JS + JSX into browser-compatible code.
  • When paired with ReactDOM, it enables rendering dynamic React components into the HTML DOM.
  • Whether via CDN or full build tool setup, Babel ensures smooth React development and browser compatibility.



Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex