02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Hello JSX

Tutorial: Setting Up and Using JSX in React.js

In this tutorial, you will learn how to set up JSX in your React.js project and understand its core functionality. JSX is a powerful syntax extension for JavaScript that makes building React components more intuitive and maintainable.


What is JSX?

JSX (JavaScript XML) is a syntax extension for JavaScript. It enables you to write HTML-like elements directly within your JavaScript code. These elements are then converted into React-specific JavaScript calls during the build process.

Key Features of JSX:

  • Combines HTML structure and JavaScript logic in a single file.
  • Makes your code more readable and maintainable.
  • Works seamlessly with React's component-based architecture.

Steps to Set Up JSX in Babel

  1. Install Babel JSX Preset:
    Add support for JSX in Babel by installing the @babel/preset-react package.

    npm install --save-dev @babel/preset-react
    
  2. Update Babel Configuration:
    Add the react preset to Babel's configuration file (e.g., .babelrc or babel.config.js):

    {
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
    
  3. Install React and ReactDOM:
    Install React and ReactDOM to handle React components and rendering:

    npm install react react-dom
    
  4. Update Webpack Configuration:
    Ensure Webpack processes JSX files correctly by including Babel Polyfill in your entry file:

    entry: ['@babel/polyfill', './src/index.js']
    

Basic Rules of JSX

  1. Single Root Element:
    JSX must have a single root element. For example:

    return (
      <div>
        <h1>Hello World</h1>
        <p>This is a paragraph.</p>
      </div>
    );
    
  2. Close All Tags:
    All elements must be properly closed, even self-closing ones:

    <img src="example.jpg" alt="Example" />
    
  3. Embed JavaScript Expressions:
    Use curly braces {} to embed JavaScript expressions within JSX:

    const name = 'React';
    return <h1>Welcome to {name}</h1>;
    

Rendering JSX to the DOM

To render JSX to the DOM, use the ReactDOM.render() method:

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <div>Hello JSX!</div>,
  document.getElementById('root')
);

Advantages of JSX in React

  • Unified Code Structure: Combines HTML and JavaScript logic in one place.
  • Improved Maintainability: Easier to track and update components.
  • Enhanced Debugging: Clearer error messages due to JSX's structure and syntax.

Summary

In this lecture, you configured Babel to support JSX, installed necessary dependencies, and explored JSX's basic syntax and usage. This setup provides the foundation for building React components efficiently.

In the next lecture, we’ll take this knowledge a step further and create our first React component using JSX.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!

Setting Things Up

Learn how to set up your development environment for mastering React reusable components, including Node.js, NPM, and Webpack configuration.

10:54

Configuring the Webpack Development Server

Learn to configure the Webpack Development Server for hot module replacement and automated workflows in React.

08:06

Hello JSX

Learn JSX, the JavaScript XML extension that simplifies React development. Set up JSX in Babel and explore its syntax and integration with React.

09:12

Creating a Component

Dive into React development by creating your first JSX component! In this lecture, you'll learn how to define an ES6 class, extend React components, and integrate JSX to build reusable React elements.

08:33

Developing in ES2016 Today with Babel

Learn how to configure Babel and Webpack to develop modern JavaScript applications using ES6 features. This lecture explains setting up Babel with the ES2015 preset.

09:46