Introduction to JSX
Learn how JSX makes it easier to describe user interfaces in React by combining JavaScript expressions with HTML-like syntax.
1. What Is JSX?
JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows developers to write HTML-like markup inside JavaScript code.
React developers commonly use JSX to describe what the user interface should look like. JSX is transformed into JavaScript instructions that React uses to create and update the UI.
JSX looks similar to HTML, but it follows some different
rules. For example, React uses className
instead of HTML's class attribute in JSX.
2. How JSX Works
JSX is transformed during the build or development process before the browser executes the resulting JavaScript.
JSX Code
Write markup inside JavaScript.
JavaScript Transformation
Vite uses the project's configured tooling to process JSX into JavaScript.
React Elements
React uses the resulting JavaScript to describe the interface.
Browser User Interface
The user sees the rendered heading or component.
3. Creating UI Without JSX
React can create UI elements without JSX by using
createElement() from the React package.
import { createElement } from 'react';
function App() {
return createElement(
'h1',
null,
'Hello React!'
);
}
export default App;
Expected Output
Hello React!
This approach works, but creating deeply nested interfaces
using repeated createElement() calls can make
code difficult to read.
4. Creating UI With JSX
JSX allows us to write the same UI using a more readable, HTML-like structure.
function App() {
return <h1>Hello React!</h1>;
}
export default App;
Expected Output
Hello React!
5. Understanding JSX Syntax
A JSX expression commonly contains an opening tag, some content, and a closing tag. JSX elements can also contain child elements and JavaScript expressions.
function App() {
return (
<div>
<h1>Welcome to React</h1>
<p>Learn React step by step.</p>
</div>
);
}
Breakdown of the Code
Opening Tag
<h1>
Starts the heading element.
Content
Welcome to React
Text displayed inside the heading.
Closing Tag
</h1>
Ends the heading element.
Parent Element
<div>
Groups the heading and paragraph.
6. Using JavaScript Expressions in JSX
We can insert JavaScript expressions into JSX by placing
them inside curly braces { }.
function App() {
const studentName = 'Priya';
const courseName = 'React Development';
const duration = 5;
return (
<div>
<h1>Welcome, {studentName}!</h1>
<p>Course: {courseName}</p>
<p>Duration: {duration} months</p>
<p>Next level: {duration + 1} months</p>
</div>
);
}
export default App;
Expected Output
Welcome, Priya!
Course: React Development
Duration: 5 months
Next level: 6 months
7. Difference Between JSX and HTML
| Feature | HTML | JSX |
|---|---|---|
| CSS class attribute | class |
className |
| Event handler | onclick |
onClick |
| JavaScript expression | Not directly embedded in ordinary HTML text this way | Use { } inside JSX |
| Style attribute | Normally written as a CSS string | Uses a JavaScript object |
| Closing tags | Depends on the HTML element | JSX elements must be properly closed |
| Multiple root elements | HTML document has a document structure | A component must return one root element or a Fragment |
8. Common JSX Examples
Example 1: className Instead of class
function App() {
return (
<div className="welcome-box">
<h2>Welcome to CIIT</h2>
</div>
);
}
Use className to specify a CSS class in JSX.
Example 2: Event Handler Naming
function App() {
function handleClick() {
console.log('Button clicked');
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
JSX event handler names use camelCase, such as
onClick. Here, handleClick
is passed as a function rather than being called while
rendering.
Example 3: Inline Styling
function App() {
return (
<h2 style={{
color: 'blue',
fontSize: '28px'
}}>
React Styling
</h2>
);
}
In JSX, the style attribute receives a JavaScript
object. CSS property names such as font-size
are written in camelCase as fontSize.
9. Basic Rules of JSX
Return One Root Element
Wrap sibling elements inside a common parent element or use a Fragment.
Close Every Element
Use matching closing tags or self-closing syntax for elements that do not have children.
Use className for CSS Classes
JSX uses className instead of
the HTML class attribute.
Use Curly Braces for Expressions
Insert JavaScript expressions inside JSX using
{ }.
Use CamelCase Event Names
Use names such as onClick and
onChange for React event handlers.
10. Practice Task
Task: Create a Student Introduction Component
Create an App component that displays
a student's name, course, and training duration.
- Create a variable named
studentName. - Create a variable named
courseName. - Create a variable named
duration. - Display their values using JSX curly braces.
- Add a CSS class using
className. - Run the application and verify the browser output.
Summary
- JSX stands for JavaScript XML.
- JSX lets developers write HTML-like markup inside JavaScript.
- Development tooling transforms JSX into executable JavaScript.
- Curly braces allow JavaScript expressions inside JSX.
- Use
classNamerather than HTML'sclass. - JSX elements must be properly closed and grouped under one root or a Fragment.
- React components commonly use JSX to describe their UI.