Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 4 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 24, 20264 min read

Mastering JSX Syntax: Writing Declarative React Components

Learn how to write valid JSX, embed JavaScript expressions, and handle DOM attributes correctly to build clean, declarative React components.

ReactJSXFrontendJavaScriptWeb Development

Previously in this course, we set up our development environment using Vite. Now that your workspace is ready, it's time to dive into the core of how we describe UI in React: JSX.

JSX stands for JavaScript XML. It’s a syntax extension that allows us to write code that looks like HTML directly inside our JavaScript functions. While it might look like HTML, it’s actually a syntactic sugar for React.createElement() calls, which makes our code much more readable and maintainable.

Writing Valid JSX

When you write JSX, you are essentially describing what the UI should look like. However, because JSX is ultimately transformed into JavaScript, there are strict rules you must follow.

The most important rule is that JSX must have a single parent element. If you want to return multiple elements, you must wrap them in a container.

JSX
// ❌ Invalid: Multiple top-level elements
return (
  <h1>Movie Title</h1>
  <p>Description</p>
);

// ✅ Valid: Wrapped in a single parent(like a div or Fragment)
return (
  <div>
    <h1>Movie Title</h1>
    <p>Description</p>
  </div>
);

If you don't want to add an extra <div> to your DOM tree, you can use a Fragment—an empty tag <>...</>—which tells React to group elements without adding a wrapper node to the rendered output.

Embedding JavaScript Expressions

The true power of JSX lies in its ability to seamlessly integrate with JavaScript. You can embed any valid JavaScript expression inside JSX by wrapping it in curly braces {}.

Think of curly braces as an "escape hatch" from JSX back into the world of JavaScript.

JSX
const movieTitle = "Inception";
const releaseYear = 2010;

function MovieHeader() {
  return (
    <header>
      {/* We can embed strings, numbers, and variables */}
      <h1>{movieTitle.toUpperCase()}</h1>
      <p>Released in: {releaseYear}</p>
      
      {/* We can even run logic */}
      <p>Age: {new Date().getFullYear() - releaseYear} years old</p>
    </header>
  );
}

JSX vs. HTML Attributes

JSX looks like HTML, but it is not HTML. Because it is JavaScript, some attribute names differ to avoid conflicts with reserved JavaScript keywords.

  1. className instead of class: Since class is a reserved keyword in JavaScript, React uses className to define CSS classes.
  2. htmlFor instead of for: Similar to class, for is a reserved keyword. Use htmlFor for <label> elements.
  3. CamelCase attributes: Attributes like tabindex become tabIndex, and onclick becomes onClick.

Example: The Movie Browser Card Let’s advance our movie browser project by creating a basic card component that uses these principles.

JSX
function MovieCard() {
  const title = "The Matrix";
  const isAvailable = true;

  return (
    <div className="movie-card">
      <h2 style={{ color: CE9178">'blue' }}>{title}</h2>
      <button disabled={!isAvailable}>
        {isAvailable ? "Watch Now" : "Coming Soon"}
      </button>
    </div>
  );
}

Notice the style attribute above: it takes an object ({{ color: 'blue' }}) rather than a CSS string. The outer braces indicate we are entering JavaScript, and the inner braces define the object literal.

Hands-on Exercise

Open your Vite project and navigate to your App.jsx file. Replace the default boilerplate with a simple movie display:

  1. Create a variable movieName with any movie title.
  2. Create an imageUrl variable with a placeholder URL.
  3. Return a <div> that contains an <h1> with the movie name, and an <img> tag.
  4. Remember: JSX requires images to have a self-closing tag (<img />) and a className instead of a class.

Common Pitfalls

  • Missing Self-Closing Tags: In HTML, tags like <br> or <img> don't always need a closing slash. In JSX, every tag must be closed. Use <br /> or <img src="..." />.
  • Rendering Objects: You cannot render a raw JavaScript object directly in JSX. If you try to do {user}, React will throw an error. You must render specific properties, like {user.name}.
  • Boolean values: Booleans like true or false are not rendered as text in JSX. If you want to show a status, use a ternary operator: {isOnline ? "Online" : "Offline"}.

Recap

JSX is our primary tool for defining UI. It allows us to write declarative code that maps directly to our components. By mastering the use of curly braces for expressions and understanding the nuances of attribute naming (like className), you've taken the first step toward building complex, dynamic interfaces.

Up next, we will move beyond single components and start Creating Static Components to build the skeleton of our movie browser.

Previous lessonSetting Up with Vite
Back to Blog

Similar Posts

ReactReactJune 24, 20263 min read

Setting Up with Vite: A Professional React Development Environment

Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.

Read more
ReactNext.js

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 4 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
June 24, 2026
4 min read

React reconciliation explained: How to optimize your DOM updates

React reconciliation is the engine behind your UI updates. Learn how DOM diffing works and how to minimize mutations to keep your React app fast.

Read more
ReactNext.jsJune 24, 20263 min read

React form handling: Controlled vs. Uncontrolled Components

React form handling doesn't have to be complex. Learn the trade-offs between controlled and uncontrolled components to decide when to sync your UI state.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    Coming soon
  • 6

    Styling Components

    Coming soon
  • 7

    Passing Data with Props

    Coming soon
  • 8

    Dynamic Movie Cards

    Coming soon
  • 9

    Component Composition

    Coming soon
  • 10

    Conditional Rendering

    Coming soon
  • 11

    Rendering Lists of Data

    Coming soon
  • 12

    The Key Prop Explained

    Coming soon
  • 13

    Introduction to React State

    Coming soon
  • 14

    Managing State with useState

    Coming soon
  • 15

    Building an Interactive Search Bar

    Coming soon
  • 16

    Handling Click Events

    Coming soon
  • 17

    Updating State Based on Previous State

    Coming soon
  • 18

    Filtering the Movie List

    Coming soon
  • 19

    Handling Form Submissions

    Coming soon
  • 20

    Controlled Components

    Coming soon
  • 21

    Event Bubbling and Propagation

    Coming soon
  • 22

    Building a Movie Filter Toggle

    Coming soon
  • 23

    Introduction to Side Effects

    Coming soon
  • 24

    useEffect Dependencies

    Coming soon
  • 25

    Fetching Data from an API

    Coming soon
  • 26

    Handling Loading States

    Coming soon
  • 27

    Managing Errors

    Coming soon
  • 28

    Cleanup Functions in useEffect

    Coming soon
  • 29

    Debouncing Search Input

    Coming soon
  • 30

    Refactoring for Clean Code

    Coming soon
  • 31

    Folder Structure Best Practices

    Coming soon
  • 32

    Extracting Custom Hooks

    Coming soon
  • 33

    Prop Drilling and Context API

    Coming soon
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course