Learn how to write valid JSX, embed JavaScript expressions, and handle DOM attributes correctly to build clean, declarative React components.
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.
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.
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.
JSXconst 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 looks like HTML, but it is not HTML. Because it is JavaScript, some attribute names differ to avoid conflicts with reserved JavaScript keywords.
className instead of class: Since class is a reserved keyword in JavaScript, React uses className to define CSS classes.htmlFor instead of for: Similar to class, for is a reserved keyword. Use htmlFor for <label> elements.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.
JSXfunction 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.
Open your Vite project and navigate to your App.jsx file. Replace the default boilerplate with a simple movie display:
movieName with any movie title.imageUrl variable with a placeholder URL.<div> that contains an <h1> with the movie name, and an <img> tag.<img />) and a className instead of a class.<br> or <img> don't always need a closing slash. In JSX, every tag must be closed. Use <br /> or <img src="..." />.{user}, React will throw an error. You must render specific properties, like {user.name}.true or false are not rendered as text in JSX. If you want to show a status, use a ternary operator: {isOnline ? "Online" : "Offline"}.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.
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.
Mastering JSX Syntax
Creating Static Components
Styling Components
Passing Data with Props
Dynamic Movie Cards
Component Composition
Conditional Rendering
Rendering Lists of Data
The Key Prop Explained
Introduction to React State
Managing State with useState
Building an Interactive Search Bar
Handling Click Events
Updating State Based on Previous State
Filtering the Movie List
Handling Form Submissions
Controlled Components
Event Bubbling and Propagation
Building a Movie Filter Toggle
Introduction to Side Effects
useEffect Dependencies
Fetching Data from an API
Handling Loading States
Managing Errors
Cleanup Functions in useEffect
Debouncing Search Input
Refactoring for Clean Code
Folder Structure Best Practices
Extracting Custom Hooks
Prop Drilling and Context API
Polishing the UI
Finalizing the Movie Browser
Review of Component Lifecycle
Review of State Management
Building a Modal Component
Introduction to PropTypes
Performance Optimization Basics
Handling Browser History
Working with LocalStorage
Building a Favorites List
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced