JSX is a syntax extension for JavaScript that looks like HTML. It lets you write UI components in a way that feels like writing HTML but with the power of JavaScript.
Click to reveal answer
beginner
Why do we use className instead of class in JSX?
In JSX, class is a reserved word in JavaScript, so React uses className to assign CSS classes to elements.
Click to reveal answer
intermediate
How do you write inline styles in JSX compared to HTML?
In JSX, inline styles are written as an object with camelCase property names, for example: {'{ color: "red", fontSize: "12px" }'}, unlike HTML which uses a string.
Click to reveal answer
intermediate
What is the difference in how attributes like for are handled in JSX?
JSX uses htmlFor instead of for because for is a reserved word in JavaScript. This is used mainly with <label> elements.
Click to reveal answer
beginner
Can JSX elements have multiple root elements like HTML?
No, JSX requires a single root element. If you want multiple elements, you must wrap them in a parent element or use React fragments.
Click to reveal answer
Which attribute is used in JSX to add CSS classes?
AcssClass
Bclass
CclassName
DstyleClass
✗ Incorrect
JSX uses 'className' instead of 'class' because 'class' is a reserved word in JavaScript.
How do you write inline styles in JSX?
AAs an object with camelCase keys
BAs a string like in HTML
CUsing CSS files only
DUsing a style tag inside JSX
✗ Incorrect
JSX inline styles are objects with camelCase property names, e.g., { color: 'red' }.
What must JSX elements have that HTML elements do not require?
AMultiple root elements
BNo attributes
COnly self-closing tags
DA single root element
✗ Incorrect
JSX requires a single root element to wrap all other elements.
Which attribute replaces 'for' in JSX when labeling form elements?
AforLabel
BhtmlFor
ClabelFor
DformFor
✗ Incorrect
'htmlFor' is used in JSX because 'for' is a reserved JavaScript word.
Explain the main differences between JSX and HTML attributes.
Think about reserved words in JavaScript and how JSX adapts HTML.
You got /4 concepts.
Describe how JSX handles multiple elements compared to HTML.
Consider how JSX compiles to JavaScript function calls.
You got /3 concepts.
Practice
(1/5)
1. Which attribute should you use in JSX to add CSS classes to an element?
easy
A. cssClass
B. class
C. className
D. styleClass
Solution
Step 1: Recall JSX attribute rules
In JSX, the HTML attribute class is replaced by className because class is a reserved word in JavaScript.
Step 2: Identify correct attribute for CSS classes
Therefore, to add CSS classes in JSX, you must use className.
Final Answer:
className -> Option C
Quick Check:
JSX uses className, not class [OK]
Hint: Remember: JSX uses className, not class for CSS classes [OK]
Common Mistakes:
Using 'class' instead of 'className' causes errors
Confusing 'className' with 'cssClass'
Trying to use 'styleClass' which doesn't exist
2. Which of the following is the correct way to write a label with a 'for' attribute in JSX?
easy
A. Name
B. Name
C. Name
D. Name
Solution
Step 1: Understand JSX attribute naming
In JSX, the HTML attribute for is replaced by htmlFor because for is a reserved word in JavaScript.
Step 2: Check correct casing and spelling
The correct attribute is htmlFor with capital F, so <label htmlFor="inputId">Name</label> is correct.
Final Answer:
<label htmlFor="inputId">Name</label> -> Option A
Quick Check:
Use htmlFor, not for in JSX labels [OK]
Hint: Use htmlFor (camelCase) instead of for in JSX labels [OK]
Common Mistakes:
Using 'for' attribute directly causes syntax errors
Writing 'htmlfor' with lowercase f is incorrect
Using non-existent attributes like 'forId'
3. What will be the rendered output of this JSX code?
const element = <input type="checkbox" checked="true" />;
ReactDOM.render(element, document.getElementById('root'));
medium
A. A checkbox input with checked attribute as string
B. A checked checkbox input
C. Syntax error due to checked attribute value
D. An unchecked checkbox input
Solution
Step 1: Analyze checked attribute in JSX
In JSX, checked="true" without curly braces sets the prop to the string "true".
Step 2: Understand rendering of boolean attributes
React renders <input type="checkbox" checked="true" />. Browsers treat the presence of the checked attribute (any non-empty value) as checked for uncontrolled checkboxes.