Home / Learn React / JSX: Markup Inside JavaScript
JSX: Markup Inside JavaScript
JSX looks like HTML but it lives inside JavaScript. It lets you write the structure of a component right next to the logic that decides what to show.
Theory in plain words
What it is
JSX is a syntax extension for JavaScript. A line such as <h1>Hi</h1> is not a string and not real HTML. A compiler changes it into React.createElement("h1", null, "Hi") or the modern jsx() call.
Why we use it
Rendering logic and markup belong together: a button, the code that runs when you click it and the text it shows are all one idea. JSX keeps them in one place and lets you use full JavaScript inside the markup.
How it works
The build tool (or the tiny compiler running on this page) converts JSX into plain JavaScript objects that describe the UI. React reads those objects and creates the real DOM elements.
Where it is used
- Every React component returns JSX
- Templates for lists, tables and forms
- Conditional UI such as logged in and logged out views
Rule 1: return one root element
A component can return only one thing. Wrap siblings in a <div> or in a Fragment <>...</> that adds no extra element to the page.
Open the browser inspector on the result: there is no wrapper div.
Rule 2: close every tag
HTML lets you write <br> and <img> without closing. JSX does not. Write <br /> and <img />. Also close <input />.
Rule 3: camelCase attributes
| HTML | JSX | Why |
|---|---|---|
class | className | class is a reserved word in JavaScript |
for | htmlFor | for is a reserved word |
onclick | onClick | Events are camelCase and take a function |
tabindex | tabIndex | camelCase |
style="color:red" | style={{ color: "red" }} | style takes an object |
The double braces are not special: the outer pair says "JavaScript here", the inner pair is an object.
Rule 4: use curly braces for JavaScript
Inside JSX, { } opens a window into JavaScript. You can put variables, function calls and expressions in them, both as text and as attribute values. You cannot put statements such as if or for in them. Use ? : and .map() instead.
Comments and text
return (
<div>
{/* this is a comment inside JSX */}
<p>Hello</p>
</div>
);- JSX removes extra spaces and line breaks around text. Use
{" "}when you need a space between two elements on one line. - Booleans,
nullandundefinedshow nothing. The number0shows0. - Text from users is escaped for you, so JSX protects you from most injection problems.
{count && <List />} shows a lone 0 when count is 0. Write {count > 0 && <List />} instead.Quick quiz
1. Which attribute do you use for a CSS class in JSX?
class is a reserved word in JavaScript, so JSX uses className.
2. What is wrong with return (<h1>A</h1><p>B</p>)?
Wrap siblings in a div or a Fragment.
3. What does {items.length && <List />} show when items is empty?
0 is a number and React renders it. Compare with > 0 instead.
4. How do you write an inline style in JSX?
style takes a JavaScript object.
Common questions
Which attribute do you use for a CSS class in JSX?
className. class is a reserved word in JavaScript, so JSX uses className.
What is wrong with return (<h1>A</h1><p>B</p>)?
A component must return one root element. Wrap siblings in a div or a Fragment.
What does {items.length && <List />} show when items is empty?
The number 0. 0 is a number and React renders it. Compare with > 0 instead.