JSX
Understand JSX — the syntax extension that makes React feel like writing HTML in JavaScript. Learn how it compiles, what rules it enforces, and how senior engineers use it in production.
Last reviewed: January 2025
🔍 What Is It?
JSX (JavaScript XML) is a syntax extension for JavaScript that lets you write UI structure inline with your logic. It looks like HTML but behaves like JavaScript.
- →JSX compiles to
React.createElement()calls (React 17+ uses the new JSX transform — no import needed) - →Every JSX element is a function call that returns a React element object
- →JSX is not a template language — it has the full power of JavaScript
- →Self-closing tags are required:
<img />,<br />,<Input /> - →
classbecomesclassName,forbecomeshtmlFor(reserved JS words) - →Inline styles are objects:
style={{ color: 'red', fontSize: 14 }}
🎯 Why Do We Need It?
Before JSX, writing React looked like this:
React.createElement('div', { className: 'card' },
React.createElement('h2', null, 'Title'),
React.createElement('p', null, 'Content')
)With JSX:
<div className="card">
<h2>Title</h2>
<p>Content</p>
</div>JSX makes component trees readable, maintainable, and reviewable. Without it, deeply nested UIs become unreadable noise. It also enables tooling — linters, formatters, and IDEs understand JSX structure.
🏗️ What Happens in Real Projects?
JSX is used in every React component in every React project. In enterprise apps:
- →Conditional rendering —
{isLoggedIn && <Dashboard />}or ternaries - →List rendering —
{items.map(item => <Card key={item.id} data={item} />)} - →Dynamic class names —
className={btn ${variant}}or libraries likeclsx - →Spreading props —
<Input {...register('email')} />(React Hook Form pattern) - →Fragments —
<><Header /><Main /><Footer /></>when no wrapper div is needed
⚙️ How It Works
Compilation pipeline:
- You write JSX in
.tsx/.jsxfiles - Babel (or SWC/esbuild) transforms JSX to
React.createElement()calls - React 17+ new JSX transform auto-imports from
react/jsx-runtime— no manualimport Reactneeded - At runtime,
createElementreturns a plain JS object:{ type, props, key, ref } - React's reconciler compares these objects to the previous render (diffing)
- Only changed DOM nodes are updated
Key rules:
- →Every JSX expression must have a single root element (or Fragment)
- →JavaScript expressions go inside
{}— not statements (noif,fordirectly) - →
null,undefined,falserender nothing — useful for conditional rendering
💻 Example
JSX Patterns You Will Use Every Day — From basic syntax to real-world conditional and list rendering patterns.
🤔 Common Confusions
❓ Is JSX HTML?
No. JSX looks like HTML but it compiles to JavaScript. Key differences: className not class, htmlFor not for, camelCase event names (onClick not onclick), self-closing required, inline styles are objects not strings. Treat JSX as JavaScript with HTML-like syntax, not HTML.
❓ Why can I not use if statements directly in JSX?
JSX curly braces accept expressions, not statements. An if block is a statement. Use ternaries (condition ? a : b) for inline conditionals, or move complex logic to a variable above the return statement. For multi-branch logic, a helper function returning JSX is clearest.
❓ Why does rendering 0 show the number on screen?
Short-circuit {count && <Component />} evaluates to 0 (falsy number) when count is 0 — and React renders 0 as text. Fix: use {count > 0 && <Component />} or {!!count && <Component />}. This is one of the most common JSX bugs in production.
🚨 Common Production Issues
**Bug: Rendering 0 unexpectedly** — {items.length && <List />} renders "0" when array is empty. Fix: {items.length > 0 && <List />}.
**Bug: Missing key prop in lists** — Causes incorrect DOM updates during reorders/deletions. Always use stable unique IDs, never array index for dynamic lists.
**Bug: Mutating objects in JSX** — Passing new object literals as props on every render ({style={{ color: 'red' }}) creates new references each render, breaking memoization. Extract to constants outside the component.
**Performance: Inline arrow functions in JSX** — <Button onClick={() => handleClick(id)} /> creates a new function every render. Fine for most cases, but breaks React.memo. Use useCallback if the child is memoized.
✅ Best Practices
Keep JSX readable — if your return statement is longer than 40 lines, extract sub-components
Use Fragments (<></>) instead of wrapper divs to avoid unnecessary DOM nodes
Always provide meaningful key props for list items — use entity IDs, never array index
Use clsx or classnames library for dynamic class names — avoids messy template literal strings
Extract complex conditional logic to named variables above the return — improves readability
Use htmlFor on labels and id on inputs — JSX accessibility matters from day one
🏛️ Architect's Note
JSX is the most read and reviewed code in your codebase — every PR reviewer reads it. Invest in JSX clarity. A component's JSX should tell a story: what is being rendered, under what conditions, with what data. If you need a comment to explain a JSX block, consider extracting it into a named sub-component instead. In large teams, consistent JSX patterns (how you handle conditionals, lists, and props) reduce cognitive load significantly. Establish a team style guide for JSX early.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
React Technology Guide
Return to the React learning hub.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Python Developer Career Path
A practical route through Python fundamentals, automation, backend basics, testing, projects, interview preparation, and career growth.
Sources
Sources / References
Use these React references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide React learning path
Browse the full React topic collection and learning order on SplashRide.
React Interview Prep
Practice React interview questions and scenario-based answers.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Python Developer Career Path
A practical route through Python fundamentals, automation, backend basics, testing, projects, interview preparation, and career growth.
Components
Master React components — the fundamental building blocks of every React application. Learn component design, composition patterns, and how senior engineers structure components in enterprise projects.
Props
Master React props — the mechanism for passing data and behaviour between components. Learn prop design, TypeScript typing, and the patterns senior engineers use in real enterprise applications.