Monday, March 3, 2025
What is JSX in React - Writing Your First JSX Program
Posted by

If you're new to React, you've probably come across the term JSX. JSX is a syntax extension for JavaScript that looks similar to XML or HTML and allows you to write HTML inside JavaScript, which makes designing UI components easier and more intuitive. JSX is a key feature of React that helps in building reusable UI components.
In this blog, you'll learn what JSX is, how it works with React, and how to write your first JSX program. Let's dive in!
What is JSX?
JSX (JavaScript XML) is a syntax extension created by Facebook for React. JSX allows you to write HTML-like code directly in JavaScript, which is then transformed into React elements. This transformation is done using tools like Babel, which is usually configured in any React project.
One of the main advantages of JSX is that it combines the conciseness of JavaScript with the familiarity of HTML-like syntax, making it easier to reason about what your UI will look like at any point in time.
Why Use JSX?
Using JSX with React comes with several benefits:
- Clean and Readable Code: JSX allows you to write templates in a declarative way, making your code more readable and easier to understand.
- Easier Debugging: Since JSX is fairly predictable and follows HTML-like syntax, it's much easier to debug.
- Expressiveness: JSX provides a powerful way to express how your UI should look and behave.
Writing JSX
Let's start by writing a simple JSX program. For this, you need to have Node.js and npm installed on your machine. If you haven't installed them yet, you can download and install them from nodejs.org.
First, create a new React project using create-react-app
. Open your terminal and run the following commands:
npx create-react-app my-jsx-app
cd my-jsx-app
npm start
The create-react-app
command sets up a new React project with all necessary dependencies. The npm start
command starts the development server, and you should see the default React welcome page in your browser.
Your First JSX Program
Let's modify the default React project to include some custom JSX. Open the src/App.js
file in your favorite code editor and replace its content with the following code:
import React from 'react';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello, JSX!</h1>
<p>
Welcome to your first JSX program.
</p>
</header>
</div>
);
}
export default App;
In this example:
- We import
React
from thereact
package. This is necessary for creating React elements. - We define a functional component called
App
. This component returns a JSX template. - The JSX template consists of an HTML-like structure inside the
return
statement of theApp
component.
Understanding the JSX Syntax
Let's break down the JSX syntax used in the example:
-
JSX Elements: JSX elements are the building blocks of React components. In the example,
<div className="App">
,<header className="App-header">
,<h1>Hello, JSX!</h1>
, and<p>Welcome to your first JSX program.</p>
are all JSX elements. -
Attributes: You can add attributes to JSX elements, just like HTML. In the example,
className
is used to add a class to thediv
andheader
elements. -
Inline Expressions: You can also embed JavaScript expressions inside JSX using curly braces
{}
. For example, you could write:function App() { const greeting = "Hello, JSX!"; return ( <div className="App"> <header className="App-header"> <h1>{greeting}</h1> <p> Welcome to your first JSX program. </p> </header> </div> ); }
In this modified example, the value of the
greeting
variable is embedded inside theh1
element.
JSX Rules
Here are some rules to keep in mind when writing JSX:
-
Self-Closing Tags: Unlike HTML, JSX elements must have a closing tag. For example:
<img src="logo.png" alt="Logo" />
-
Nesting Elements: JSX elements can be nested within each other, like in HTML. Ensure that the elements are properly nested to avoid syntax errors.
<div> <h1>Title</h1> <p>Description</p> </div>
-
Camel Case Attributes: In JSX, HTML attributes are written in camelCase instead of kebab-case. For example:
<label htmlFor="name">Name:</label> <input type="text" id="name" />
-
Comments: You can add comments in JSX by wrapping them in curly braces and using the JavaScript comment syntax. For example:
<div> {/* This is a comment in JSX */} <h1>Hello, JSX!</h1> </div>
Integrating JSX in a React Component
Now that you understand the basics of JSX, let's create a more complex component that uses JSX. We'll create a simple component that displays a list of items.
First, create a new file called ItemList.js
inside the src
directory. Then, add the following code:
import React from 'react';
const ItemList = () => {
const items = ['Apple', 'Banana', 'Cherry'];
return (
<div>
<h2>Fruit List</h2>
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
);
};
export default ItemList;
In this example:
- We define a functional component called
ItemList
. - We create an array of items and use the
map
function to iterate over the array and return a list ofli
elements. - Each
li
element has akey
attribute, which is a unique identifier for React to identify which item has changed, is added, or is removed.
Next, import the ItemList
component into the App.js
file and use it inside the App
component:
import React from 'react';
import ItemList from './ItemList';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello, JSX!</h1>
<p>
Welcome to your first JSX program.
</p>
</header>
<ItemList />
</div>
);
}
export default App;
Now, when you check your application in the browser, you should see the "Fruit List" with items "Apple", "Banana", and "Cherry".
Key Differences Between HTML and JSX
Here are some key differences between HTML and JSX that you should be aware of:
Feature | HTML | JSX |
---|---|---|
Element Attributes | Use class for CSS classes | Use className for CSS classes |
JavaScript Expressions | Not supported directly | Embedded using curly braces {expression} |
Self-Closing Tags | Can be written without a closing tag | Must have a closing tag, e.g., <img /> |
React Components | Not applicable | Used to create reusable UI components |
HTML Entities | Use entities like < for special characters | Use curly braces with string literals, e.g., {'<'} |
Tips for Writing Clean JSX
Here are some tips to help you write cleaner and more maintainable JSX code:
- Consistent Formatting: Use consistent indentation and spacing to make your code easier to read.
- Avoid Deep Nesting: Limit the depth of nesting in your JSX to improve readability.
- Use Shorthand: Use arrow functions and shorthand syntax where possible to reduce boilerplate code.
- Component Reusability: Break down your UI into small, reusable components to keep your code organized.
Conclusion
JSX is a powerful feature of React that allows you to write clean and readable code. By embedding HTML-like syntax directly in JavaScript, JSX makes it easier to create complex UIs and manage the state of your application.
In this blog, you learned what JSX is, how it works, and how to write your first JSX program. You also saw how to integrate JSX with React components and compared JSX with HTML to understand their differences.
Practice writing more complex components using JSX, and you'll get a better understanding of how it can enhance your React development workflow.
Additional Resources
- JSX in Depth - official React documentation on JSX.
- JSX Tutorial - a comprehensive tutorial on JSX by W3Schools.
- JSX Compilation - how JSX is compiled into React.createElement calls.
By following this blog and using the provided resources, you'll be well on your way to becoming proficient in using JSX with React. Happy coding!