Lesson 11 of 60 – JSX Introduction
18%

JSX Introduction

JSX is one of the most important concepts in React. It allows us to write HTML-like markup directly inside JavaScript code. JSX makes React components easier to read and write.

Note: JSX looks similar to HTML, but it is not exactly HTML. JSX is a syntax extension commonly used with JavaScript in React applications.

1. What is JSX?

JSX stands for JavaScript XML. It allows developers to write HTML-like elements inside JavaScript code.

function App() {

    return (
        <h1>Hello React</h1>
    );

}

The <h1> element is written directly inside the JavaScript function.

2. Why Use JSX?

JSX makes it easier to describe what the user interface should look like.

function App() {

    return (
        <div>
            <h1>Welcome</h1>
            <p>Learn React</p>
        </div>
    );

}

The structure of the user interface is easy to understand from the code.

3. JSX Looks Like HTML

JSX uses HTML-like tags such as headings, paragraphs, buttons, and div elements.

<h1>Hello</h1>

<p>Welcome to React</p>

<button>Click Me</button>

This makes JSX familiar to developers who already know HTML.

4. JSX Inside JavaScript

JSX can be returned from a JavaScript function.

function Welcome() {

    return (
        <h2>Welcome to My Website</h2>
    );

}

Here, Welcome is a JavaScript function that returns JSX.

5. JSX Is Not HTML

Although JSX looks like HTML, it follows some different rules. For example, JSX uses className instead of the HTML class attribute.

<div className="box">
    Hello
</div>

6. JSX Must Have One Parent Element

When returning multiple JSX elements, they normally need to be contained inside one parent element.

function App() {

    return (
        <div>
            <h1>Hello</h1>
            <p>Welcome</p>
        </div>
    );

}

7. Incorrect Multiple Root Elements

The following structure has two sibling root elements:

return (
    <h1>Hello</h1>
    <p>Welcome</p>
);

Instead, place them inside a parent element or use a React Fragment.

8. Using a div as Parent

A simple way to group multiple JSX elements is to use a div.

return (
    <div>
        <h1>Hello</h1>
        <p>Learn React</p>
    </div>
);

9. JSX Fragments

A React Fragment can group multiple elements without adding an extra HTML element to the page.

return (
    <>
        <h1>Hello</h1>
        <p>Welcome</p>
    </>
);

10. JSX Expressions

JavaScript expressions can be written inside JSX using curly braces.

function App() {

    let name = "Rahul";

    return (
        <h1>Hello {name}</h1>
    );

}

The value of name is displayed inside the heading.

11. Displaying Numbers in JSX

JavaScript numbers can also be displayed using curly braces.

function App() {

    let age = 20;

    return (
        <p>Age: {age}</p>
    );

}

12. Performing Calculations

JavaScript expressions can perform calculations inside JSX.

function App() {

    let a = 10;
    let b = 20;

    return (
        <h2>Total: {a + b}</h2>
    );

}

The result displayed on the page is 30.

13. JSX with Variables

Variables can be created in JavaScript and then displayed in JSX.

function App() {

    const course = "React";

    return (
        <h1>Learning {course}</h1>
    );

}

14. JSX with JavaScript Expressions

function App() {

    const price = 500;
    const quantity = 2;

    return (
        <p>
            Total: {price * quantity}
        </p>
    );

}

Expressions inside JSX can use variables and JavaScript operators.

15. JSX Comments

JavaScript-style comments can be written inside JSX expressions.

function App() {

    return (
        <div>
            {/* This is a JSX comment */}
            <h1>Hello React</h1>
        </div>
    );

}

16. JSX Attributes

JSX elements can have attributes similar to HTML elements.

<img
    src="logo.png"
    alt="Logo"
/>

JSX attributes are used to provide additional information to elements.

17. className in JSX

In JSX, the HTML class attribute is normally written as className.

<div className="container">
    <h1>Hello React</h1>
</div>

18. JSX Self-Closing Elements

Elements without child content can be written using a self-closing syntax.

<img src="image.jpg" alt="Image" />

<input type="text" />

<br />

The closing slash is important for JSX self-closing elements.

19. JSX Naming Convention

JSX uses JavaScript-based naming conventions for many properties. For example, className is used instead of class.

<button className="btn">
    Click Me
</button>

20. JSX and CSS Classes

CSS classes can be applied using className.

function App() {

    return (
        <div className="card">
            <h2>React Course</h2>
        </div>
    );

}

The class can then be styled using CSS.

21. JSX Event Example

JSX can also contain event attributes such as onClick.

function App() {

    function showMessage() {
        alert("Hello!");
    }

    return (
        <button onClick={showMessage}>
            Click Me
        </button>
    );

}

22. JSX with Conditional Expressions

JavaScript conditional expressions can be used inside JSX.

function App() {

    const isLoggedIn = true;

    return (
        <h2>
            {isLoggedIn ? "Welcome" : "Please Login"}
        </h2>
    );

}

23. JSX with Arrays

JavaScript arrays can be used to create dynamic content in React.

function App() {

    const names = ["Amit", "Ravi", "Neha"];

    return (
        <div>
            {names.map(name => (
                <p key={name}>{name}</p>
            ))}
        </div>
    );

}

24. JSX Expressions vs Strings

Text can be written directly, while JavaScript values are generally placed inside curly braces.

<h1>Hello React</h1>

<h1>Hello {name}</h1>

The second example uses the JavaScript variable name.

25. JSX Can Be Nested

JSX elements can be placed inside other JSX elements.

<div>

    <h1>React Course</h1>

    <div>
        <p>Learn JSX</p>
    </div>

</div>

26. JSX and React Components

Components commonly return JSX that describes their user interface.

function Welcome() {

    return (
        <div>
            <h1>Welcome</h1>
            <p>Learn React with JSX</p>
        </div>
    );

}

This JSX becomes part of the component's rendered output.

27. JSX Is Transformed

JSX is not directly understood by the JavaScript engine as normal JavaScript syntax. The React development toolchain transforms JSX into JavaScript that can create the required React elements.

JSX
 ↓
Transformation
 ↓
JavaScript
 ↓
React Elements
 ↓
User Interface

28. Simple JSX Example

function App() {

    const name = "Rahul";

    return (
        <div>
            <h1>Hello {name}</h1>
            <p>Welcome to React.</p>
            <button>Learn More</button>
        </div>
    );

}

export default App;

29. JSX Best Practices

  • Keep JSX readable and properly formatted.
  • Use one parent element when required.
  • Use fragments when an extra HTML element is unnecessary.
  • Use className instead of HTML class.
  • Close JSX elements properly.
  • Use curly braces for JavaScript expressions.
  • Break large components into smaller components.
  • Use meaningful names for variables and components.

30. Complete JSX Example

function App() {

    const student = "Rahul";
    const course = "React";

    return (
        <div className="container">

            <h1>Welcome {student}</h1>

            <p>
                You are learning {course}.
            </p>

            <button className="btn">
                Start Learning
            </button>

        </div>
    );

}

export default App;

This example combines JSX elements, variables, expressions, attributes, and a CSS class in a React component.

📌 Key Points

  • JSX stands for JavaScript XML.
  • JSX allows HTML-like markup inside JavaScript code.
  • JSX is commonly used to describe React user interfaces.
  • JSX is similar to HTML but has different rules.
  • Multiple JSX elements normally need one parent element.
  • React Fragments can group elements without adding an extra HTML element.
  • JavaScript expressions can be written inside curly braces.
  • Use className instead of class in JSX.
  • JSX elements should be properly closed.
  • Self-closing elements use a closing slash.
  • JSX can contain variables, calculations, conditions, and function calls.
  • JSX is transformed into JavaScript by the React development toolchain.

🧠 Quick Quiz

Question: What is JSX mainly used for in React?