DEV Community

TenE
TenE

Posted on

2 1 1 2 1

JSX in React: Writing HTML Inside JavaScript Explained

What is JSX?

JSX (JavaScript XML) is a syntax extension that allows you to write HTML inside JavaScript. It makes React components more readable and expressive.

JSX Example:

const element = <h1>Hello, JSX!</h1>;
Enter fullscreen mode Exit fullscreen mode

Why Use JSX?

  1. Readable & Intuitive – Looks like HTML inside JavaScript.
  2. Prevents XSS Attacks – JSX escapes values automatically.
  3. Full JavaScript Power – You can use JavaScript inside JSX.

JSX Syntax Rules

1. Only One Parent Element

Correct:

return (
  <div>
    <h1>Hello</h1>
    <p>Welcome to React</p>
  </div>
);
Enter fullscreen mode Exit fullscreen mode

Incorrect:

return (
  <h1>Hello</h1>
  <p>Welcome to React</p>
);
Enter fullscreen mode Exit fullscreen mode

Fix: Wrap elements inside a <div> or <> (fragment).


2. Use className Instead of class

Correct:

return <h1 className="title">Hello, JSX!</h1>;
Enter fullscreen mode Exit fullscreen mode

Incorrect:

return <h1 class="title">Hello, JSX!</h1>;
Enter fullscreen mode Exit fullscreen mode

Why? class is a reserved keyword in JavaScript.


3. Expressions in JSX Must Be Inside {}

Correct:

const name = "React";
return <h1>Welcome to {name}!</h1>;
Enter fullscreen mode Exit fullscreen mode

Incorrect:

const name = "React";
return <h1>Welcome to name!</h1>;
Enter fullscreen mode Exit fullscreen mode

4. Self-Closing Tags for Elements Without Children

Correct:

return <img src="logo.png" alt="React Logo" />;
Enter fullscreen mode Exit fullscreen mode

Incorrect:

return <img src="logo.png" alt="React Logo"></img>;
Enter fullscreen mode Exit fullscreen mode

Using JavaScript Inside JSX

Example: Dynamic Content in JSX

const age = 25;
return <p>{age >= 18 ? "Adult" : "Minor"}</p>;
Enter fullscreen mode Exit fullscreen mode

Example: Calling Functions in JSX

function getGreeting(name) {
  return `Hello, ${name}!`;
}

return <h1>{getGreeting("React")}</h1>;
Enter fullscreen mode Exit fullscreen mode

JSX Without Babel (Pure React)

Under the hood, JSX compiles into React.createElement().

JSX:

const element = <h1>Hello, JSX!</h1>;
Enter fullscreen mode Exit fullscreen mode

Compiles to:

const element = React.createElement("h1", null, "Hello, JSX!");
Enter fullscreen mode Exit fullscreen mode

JSX just makes writing React easier!


Summary

  • JSX lets you write HTML inside JavaScript.
  • Must have one parent element.
  • Use {} for JavaScript expressions.
  • Use className instead of class.
  • Self-close elements like <img />.

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

Top comments (0)

👋 Kindness is contagious

Enjoyed this post? Please give a ❤️ or leave a supportive comment!

Okay