📖 Scenario: You are building a simple React component to display a user profile card. You want to practice how JSX syntax differs from regular HTML.
🎯 Goal: Create a React functional component called UserProfile that renders a user card using JSX. You will learn how to write JSX attributes and elements that differ from HTML.
📋 What You'll Learn
Create a React functional component named UserProfile
Use JSX syntax to create a div container with a class name
Add an img tag with the correct JSX attribute for the image source
Add a label element with the correct JSX attribute for htmlFor
Use camelCase style for event handler attributes in JSX
💡 Why This Matters
🌍 Real World
React developers write JSX to create user interfaces that look like HTML but have special syntax rules.
💼 Career
Understanding JSX syntax differences is essential for React developers to build accessible and maintainable web apps.
Progress0 / 4 steps
1
Create the React component skeleton
Create a React functional component called UserProfile that returns a div with the class name profile-card. Use JSX syntax for the class name attribute.
React
Hint
Remember in JSX, use className instead of class for CSS classes.
2
Add an image with JSX source attribute
Inside the div, add an img tag with the source set to "https://example.com/avatar.png" using the JSX attribute src. Also add an alt attribute with the text User Avatar.
React
Hint
In JSX, src and alt attributes work like in HTML.
3
Add a label with JSX htmlFor attribute
Below the image, add a label element with the text Username:. Use the JSX attribute htmlFor set to username to link it to an input.
React
Hint
In JSX, use htmlFor instead of for in labels.
4
Add an input with JSX event handler
Add an input element of type text with the id username. Add an onChange attribute with an empty arrow function () => {} to show JSX uses camelCase for event handlers.
React
Hint
JSX uses camelCase for event handlers like onChange.
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.