JSX lets you write HTML-like code inside JavaScript. It makes building user interfaces easier and clearer.
JSX syntax rules in React
const element = <tagName attributeName={value}>Content</tagName>;Use curly braces {} to insert JavaScript expressions inside JSX.
Every JSX tag must be closed, either with a closing tag or self-closing slash.
const greeting = <h1>Hello, world!</h1>;const name = 'Alice'; const greeting = <h1>Hello, {name}!</h1>;
const image = <img src="logo.png" alt="Logo" />;
const list = <ul> <li>Item 1</li> <li>Item 2</li> </ul>;
This React component uses JSX to create a welcome message. It shows how to insert a variable, use semantic HTML tags, and add accessibility with aria-label and alt attributes.
import React from 'react'; function WelcomeMessage() { const userName = 'Sam'; return ( <section aria-label="welcome section"> <h2>Welcome, {userName}!</h2> <p>We are glad to see you here.</p> <img src="welcome.png" alt="Welcome illustration" /> </section> ); } export default WelcomeMessage;
JSX must have one parent element wrapping all content.
Use camelCase for attribute names like className instead of class.
Comments inside JSX use curly braces and JavaScript comment syntax: {/* comment */}.
JSX lets you write HTML-like code inside JavaScript for React UI.
Use curly braces {} to add JavaScript expressions inside JSX.
Always close tags and use one parent element per JSX block.
Practice
Solution
Step 1: Understand JSX expression embedding
JSX uses curly braces {} to embed JavaScript expressions inside HTML-like code.Step 2: Check each option for correct syntax
<h1>Hello, {name}!</h1>correctly uses curly braces around the variablename. The other options use invalid syntax for embedding expressions.Final Answer:
<h1>Hello, {name}!</h1> -> Option AQuick Check:
Curly braces for JS expressions =<h1>Hello, {name}!</h1>[OK]
- Using parentheses () instead of curly braces
- Using $ sign like template strings inside JSX
- Adding extra symbols like & before curly braces
Solution
Step 1: Check self-closing tags in JSX
In JSX, tags likeinput,img, andbrmust be self-closed with a slash/.Step 2: Validate each option
<input type='text' />correctly self-closesinput.<div><img src='logo.png'></div>misses self-closing onimg.<p>Hello<p>has an unclosedptag.<br>misses self-closing onbr.Final Answer:
<input type='text' /> -> Option CQuick Check:
Self-close void tags with / =<input type='text' />[OK]
- Forgetting the slash on self-closing tags
- Leaving tags unclosed
- Using HTML void tags without self-closing
const name = 'Alice';
const element = <div>Hello, {name.toUpperCase()}!</div>;Solution
Step 1: Evaluate the JavaScript expression inside JSX
The expression{name.toUpperCase()}calls thetoUpperCase()method on the string 'Alice', resulting in 'ALICE'.Step 2: Understand JSX rendering
JSX replaces the expression with its evaluated result, so the rendered output is<div>Hello, ALICE!</div>.Final Answer:
<div>Hello, ALICE!</div> -> Option BQuick Check:
JS expression evaluated =<div>Hello, ALICE!</div>[OK]
- Expecting JSX to show raw JavaScript code
- Confusing string methods inside JSX
- Thinking JSX does not evaluate expressions
const element = <div> <p>Welcome</p> <p>User</p> </div>; const wrongElement = <p>Hello</p> <p>World</p>;
Solution
Step 1: Analyze the JSX structure
The first element uses a single parentdivwrapping twoptags, which is correct. The second snippet has two siblingptags without a parent wrapper.Step 2: Recall JSX rules about parent elements
JSX requires one parent element per block. Multiple sibling elements must be wrapped in a single parent or a fragment.Final Answer:
JSX must have only one parent element per block -> Option DQuick Check:
One parent element rule = JSX must have only one parent element per block [OK]
- Leaving sibling JSX elements unwrapped
- Thinking multiple root elements are allowed
- Confusing self-closing tags with parent wrapping
const names = ['Anna', 'Bob', 'Cara'];
A) <ul>{names.map(name => <li>{name}</li>)}</ul>
B) <ul>{names.forEach(name => <li>{name}</li>)}</ul>
C) <ul>{names.map(name => <li>name</li>)}</ul>
D) <ul>{names.map(name => <li>{name.toUpperCase()}</li>)}</ul>Solution
Step 1: Understand how to render lists in JSX
Usemapto transform array items into JSX elements. Each item must be embedded inside curly braces.Step 2: Evaluate each option
The snippet{names.map(name => <li>{name}</li>)}correctly usesmapand embeds{name}. TheforEachsnippet returnsundefined, so no list items render. The snippet<li>name</li>renders the literal string 'name' because it lacks curly braces. ThetoUpperCase()snippet uses valid JSX syntax but transforms names to uppercase, which is not the original list.Final Answer:
Correctly maps names to list items with JSX expressions -> Option AQuick Check:
Use map with curly braces for list rendering = Correctly maps names to list items with JSX expressions [OK]
- Using forEach instead of map for rendering
- Forgetting curly braces around variables
- Rendering variable names as plain text
Ready to prove it?
Pass the test and this pattern moves unknown
