JSX Deep Dive
📖 Concept
JSX (JavaScript XML) is a syntax extension for JavaScript. It looks like HTML but comes with the full power of JavaScript.
How JSX works:
Browsers cannot read JSX. It must be transformed into regular JavaScript. Modern tools like Vite or Babel use react/jsx-runtime to transform JSX into _jsx() function calls.
Key Rules of JSX:
- Return a Single Root Element: Every component must return exactly one root element (or a Fragment). This is because a function can only return one value.
- Close All Tags: Unlike HTML, all tags in JSX must be self-closed (e.g.,
<img />) or have a closing tag. - camelCase Everything: Since JSX is JS, attributes are camelCase.
classbecomesclassName,onclickbecomesonClick, andtabindexbecomestabIndex. - JavaScript in Curly Braces: You can embed any valid JS expression inside
{ }.
Wait, why not just use HTML? JSX allows you to keep your logic (JS) and your markup (UI) in the same place. This "Locality of Reference" makes components easier to understand and maintain compared to the traditional separation of HTML and JS files.
💻 Code Example
1import React from 'react';23function JsxShowcase() {4 const user = {5 name: 'Jane Doe',6 avatar: 'https://i.pravatar.cc/150?u=jane',7 isOnline: true8 };910 const getStatusColor = (online) => online ? 'bg-green-500' : 'bg-gray-500';1112 return (13 <div className="max-w-sm rounded overflow-hidden shadow-lg p-4 bg-white">14 {/* 1. Attributes are camelCase (className) */}15 <div className="flex items-center space-x-4">1617 {/* 2. Self-closing tags are required */}18 <img19 className="w-12 h-12 rounded-full"20 src={user.avatar}21 alt={user.name}22 />2324 <div className="flex-1">25 {/* 3. Expressions in curly braces */}26 <h3 className="text-lg font-semibold">{user.name}</h3>2728 <div className="flex items-center space-x-2">29 {/* 4. Conditional rendering inside JSX */}30 <span className={`w-3 h-3 rounded-full ${getStatusColor(user.isOnline)}`}></span>31 <span className="text-sm text-gray-600">32 {user.isOnline ? 'Active Now' : 'Offline'}33 </span>34 </div>35 </div>36 </div>3738 {/* 5. Using logical && for short-circuiting */}39 {user.isOnline && (40 <button className="mt-4 w-full bg-blue-500 text-white py-2 rounded hover:bg-blue-600">41 Send Message42 </button>43 )}44 </div>45 );46}4748export default JsxShowcase;
🏋️ Practice Exercise
- Convert a snippet of plain HTML (like a navigation bar) into JSX.
- Write a component that renders an array of user names into an unordered list using
.map(). - Try to return two
<div>elements from a component without a parent or a Fragment and see what error you get. - Use the 'Babel REPL' (online) to see what a JSX block looks like after it's compiled to plain JavaScript.
⚠️ Common Mistakes
Using 'class' instead of 'className'.
Forgetting to wrap multiple elements in a Fragment or a parent div.
Putting a block-level statement (like a 'for' loop or 'if/else') inside curly braces (only expressions are allowed).
Not self-closing tags like
or .
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for JSX Deep Dive