If you’re learning React, then it’s essential to learn about JSX in React. JSX is a powerful feature of React which allows you to write HTML-like syntax in JavaScript. It makes React components more readable and maintainable.
In this post, you’ll learn everything about JSX, from its syntax to best practices.
Before we get started, don’t forget to subscribe to my newsletter!
Get the latest tips, tools, and resources to level up your web development skills, delivered straight to your inbox. Subscribe here!
Now let’s jump right into it!🚀
What is JSX?
JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML inside JavaScript. It makes code cleaner compared to using React.createElement()
.
Example without JSX:
return (
React.createElement("h1", {}, "Hello, World!");
);
Example with JSX:
return (
<h1>Hello, World!</h1>;
);
Now you can see how JSX makes code more clean and readable.
Important JSX Rules
To write better JSX, keep these rules in mind:
1. One Parent Element is Required
It’s a must to wrap multiple elements inside a single parent element.
Incorrect:
return (
<h1>Hello</h1>
<p>Welcome to React!</p>
);
Always wrap the elements in a Correct: Or you can use short syntax (React Fragment <>>): If you want to write any expression of JavaScript inside JSX, then wrap the JavaScript expression inside {}. For example: This will render Note: You cannot use statements like In JSX, you can’t write HTML-style attributes. Instead, you have to use camelCase format. For example: You can’t write In JSX, the style attribute uses an object with camelCase properties. For example: Note: Always use camelCase properties. For example, use backgroundColor not background-color. Use ternary operators or Using a Ternary Operator: Using Use For example: In HTML, you use Self-closing elements like JSX doesn’t directly run in the browser. The Babel compiler converts JSX code into JSX code: Babel converts it like this: This proves that JSX is just syntactic sugar, making writing React code easier. You can use JSX inside functional components. And this can be used in other components like this: This will render That’s all for today! For paid collaboration connect with me at : [email protected] I hope this list helps you. 🚀 If you found this post helpful, here’s how you can support my work: Keep coding & happy learning!
.
return (
<div>
<h1>Hello</h1>
<p>Welcome to React!</p>
</div>
);
return (
<>
<h1>Hello</h1>
<p>Welcome to React!</p>
</>
);
2. Write JavaScript Expressions inside {}
const name = "Shefali";
return <h1>Hello, {name}!</h1>;
"Hello, Shefali!"
.if-else
directly inside {}.
3. Attribute Names Must Use CamelCase
onclick
, instead write onClick
.
// Incorrect:
return (
<button onclick="handleClick()">Click Me</button> // HTML-style syntax
);
// Correct:
return (
<button onClick={handleClick}>Click Me</button>
);
4. Inline Styles are JavaScript Objects
const styleObj = { color: "blue", fontSize: "20px" };
return (
<p style={styleObj}>Styled Text</p>;
);
// OR
return (
<p style={{ color: "blue", fontSize: "20px" }}>Styled Text</p>;
);
5. Conditional Rendering in JSX
&&
for conditions.
const isLoggedIn = true;
return <h1>{isLoggedIn ? "Welcome Back!" : "Please Sign In"}</h1>;
&&
Operator:
const showMessage = true;
return <>{showMessage && <p>Hello, User!</p>}>;
6. Rendering Lists in JSX
.map()
to render lists, and always provide a key
prop.
const names = ["Alice", "Bob", "Charlie"];
return (
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
);
7.
class
Attribute is className
in JSX
class
, but in JSX, it’s className
.
return <h1 className="heading">Hello, JSX!</h1>;
8. Self-Closing Tags Must Have
/
and must have a
/
in JSX.
return <img src="image.jpg" alt="Example" />;
JSX Compilation Process
React.createElement()
.
return (
<h1>Hello, JSX!</h1>;
);
return (
React.createElement("h1", {}, "Hello, JSX!");
);
Why is Compilation Needed?
Using JSX in Functional Components
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
<Welcome name="Shefali" />
"Hello, Shefali!"
.
JSX vs Traditional JavaScript
Feature
JSX (Recommended)
Traditional JavaScript
Syntax
Hello
React.createElement('h1', {}, 'Hello')
Readability
Clean & Simple
Complex & Verbose
Performance
Optimized via Babel
Same (After Compilation)
Debugging
Easy to Debug
Harder to Read
🎯Wrapping Up
☕ Buy me a coffee – Every little contribution keeps me motivated!
📩 Subscribe to my newsletter – Get the latest tech tips, tools & resources.
𝕏 Follow me on X (Twitter) – I share daily web development tips & insights.