What is JSX?
JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.
JSX Syntax
With JSX, you can write what looks like HTML directly in your JavaScript code:
const myElement = <h1>I Love JSX!</h1>;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
JSX allows you to write HTML elements in JavaScript and place them in the DOM without using createElement() or appendChild() methods.
JSX vs Regular JavaScript
Without JSX, you would have to write:
const myElement = React.createElement('h1', {}, 'I do NOT use JSX!');
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
Expressions in JSX
With JSX you can write expressions inside curly braces { }. The expression can be a React variable, or property, or any other valid JavaScript expression:
const myElement = <h1>React is {5 + 5} times better with JSX</h1>;
More examples:
const name = "John";
const element = <h1>Hello, {name}!</h1>;
const user = {
firstName: 'Jane',
lastName: 'Doe'
};
const greeting = <h1>Hello, {user.firstName} {user.lastName}!</h1>;
Inserting a Large Block of HTML
To write HTML on multiple lines, put the HTML inside parentheses:
const myElement = (
<div>
<h1>I am a Header.</h1>
<p>I am a paragraph.</p>
</div>
);
One Top Level Element
The HTML code must be wrapped in ONE top level element. If you want to return two elements, wrap them in a parent element:
const myElement = (
<h1>I am a Header.</h1>
<p>I am a paragraph.</p>
);
const myElement = (
<div>
<h1>I am a Header.</h1>
<p>I am a paragraph.</p>
</div>
);
React Fragments
Alternatively, you can use a "fragment" to wrap multiple lines without adding extra nodes to the DOM:
const myElement = (
<>
<p>I am a paragraph.</p>
<p>I am another paragraph.</p>
</>
);
Fragments let you group elements without adding extra DOM nodes, keeping your HTML cleaner!
Elements Must be Closed
JSX follows XML rules, and therefore HTML elements must be properly closed:
const myElement = <input type="text" />;
<input>, <img>, and <br> must end with /> in JSX!
Attribute class = className
The class attribute is a much used attribute in HTML, but since JSX is rendered as JavaScript, and the class keyword is a reserved word in JavaScript, you are not allowed to use it in JSX.
Use attribute className instead:
const myElement = <h1 className="myclass">Hello World</h1>;
Conditions in JSX
React supports if statements, but not inside JSX. You have several options:
Option 1: Outside JSX
const x = 5;
let text = "Goodbye";
if (x < 10) {
text = "Hello";
}
const myElement = <h1>{text}</h1>;
Option 2: Ternary Expression
const x = 5;
const myElement = <h1>{x < 10 ? "Hello" : "Goodbye"}</h1>;
Option 3: Logical && Operator
const isLoggedIn = true;
const myElement = (
<div>
{isLoggedIn && <h1>Welcome back!</h1>}
</div>
);
JSX Attributes
JSX uses camelCase for attribute names:
onclickbecomesonClickonchangebecomesonChangetabindexbecomestabIndexclassbecomesclassNameforbecomeshtmlFor
const element = (
<button onClick={handleClick} className="btn">
Click Me
</button>
);
Practice Example
Here's a complete example combining multiple JSX concepts:
function Welcome() {
const name = "Sarah";
const isLoggedIn = true;
const age = 25;
return (
<>
<h1 className="title">Hello, {name}!</h1>
{isLoggedIn && <p>Welcome back!</p>}
<p>You are {age >= 18 ? "an adult" : "a minor"}.</p>
<img src="avatar.jpg" alt="User avatar" />
</>
);
}
What's Next?
Now that you understand JSX, you're ready to learn about React Components - the building blocks of React applications!