Bird
Raised Fist0
Reactframework~10 mins

JSX syntax rules in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - JSX syntax rules
Start JSX
↓
Open Tag
↓
Add Attributes?
Yes→Parse Attributes
↓
Validate Attribute Names
↓
Add Children?
Yes→Parse Children
↓
Validate Children Types
↓
Close Tag
↓
JSX Element Created
JSX starts with an opening tag, optional attributes, optional children, and a closing tag to create a React element.
Execution Sample
React
const element = <button disabled={true}>Click me</button>;
Creates a button element with a disabled attribute and text child.
Execution Table
StepJSX PartActionResultNotes
1Start JSXBegin parsing JSX expressionReady to parse elementStart of JSX element
2Open Tag <button>Recognize element typeElement type set to 'button'Tag name must be valid HTML or component
3Attribute disabled={true}Parse attribute and valueAttribute 'disabled' set to boolean trueJSX allows expressions in {}
4Children 'Click me'Parse text childChild is string 'Click me'Text children must be inside tags
5Close Tag </button>Match closing tagElement closed properlyTags must be balanced
6JSX Element CreatedCreate React element objectElement ready for renderingJSX compiles to React.createElement call
7EndParsing completeJSX expression assigned to variableNo syntax errors
💡 JSX parsing stops after closing tag and element creation
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
elementTypeundefined'button''button''button''button''button'
attributes{}{}{ disabled: true }{ disabled: true }{ disabled: true }{ disabled: true }
children[][][]['Click me']['Click me']['Click me']
jsxElementundefinedundefinedundefinedundefinedundefinedReact element object
Key Moments - 3 Insights
Why must JSX tags be properly closed?
JSX requires tags to be balanced (see Step 5) because it compiles to JavaScript calls that expect matching opening and closing tags. Unclosed tags cause syntax errors.
Can attribute values be expressions?
Yes, attributes can use curly braces {} to embed JavaScript expressions (Step 3). For example, disabled={true} sets a boolean value, not a string.
What types of children can JSX elements have?
Children can be strings, numbers, other JSX elements, or expressions (Step 4). Text must be inside tags, not outside.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'attributes' after Step 3?
A{ disabled: true }
B{}
C{ disabled: 'true' }
Dundefined
💡 Hint
Check the 'attributes' row in variable_tracker after Step 3
At which step does JSX parsing confirm the element is properly closed?
AStep 3
BStep 2
CStep 5
DStep 4
💡 Hint
Look for 'Close Tag' action in execution_table
If the attribute disabled={false} was used instead, how would 'attributes' change after Step 3?
A{ disabled: true }
B{ disabled: false }
C{}
Dundefined
💡 Hint
Attributes reflect the exact value inside curly braces as shown in Step 3
Concept Snapshot
JSX syntax rules:
- Use opening and closing tags properly
- Attributes can be strings or JS expressions in {}
- Children can be text, elements, or expressions
- JSX compiles to React.createElement calls
- Tags must be balanced and valid
- Curly braces embed JS inside JSX
Full Transcript
JSX syntax starts by reading an opening tag, then optional attributes which can be JavaScript expressions inside curly braces. Next, it reads children which can be text or other JSX elements. Finally, it reads a matching closing tag. This process creates a React element object. Attributes like disabled={true} are parsed as boolean true, not strings. Tags must be properly closed to avoid syntax errors. Children must be inside tags. This flow ensures JSX compiles correctly to React.createElement calls for rendering.

Practice

(1/5)
1. Which of the following is correct JSX syntax for embedding a JavaScript expression inside HTML?
easy
A. <h1>Hello, {name}!</h1>
B. <h1>Hello, (name)!</h1>
C. <h1>Hello, $name!</h1>
D. <h1>Hello, &{name}!</h1>

Solution

  1. Step 1: Understand JSX expression embedding

    JSX uses curly braces {} to embed JavaScript expressions inside HTML-like code.
  2. Step 2: Check each option for correct syntax

    <h1>Hello, {name}!</h1> correctly uses curly braces around the variable name. The other options use invalid syntax for embedding expressions.
  3. Final Answer:

    <h1>Hello, {name}!</h1> -> Option A
  4. Quick Check:

    Curly braces for JS expressions = <h1>Hello, {name}!</h1> [OK]
Hint: Use curly braces {} to embed JavaScript in JSX [OK]
Common Mistakes:
  • Using parentheses () instead of curly braces
  • Using $ sign like template strings inside JSX
  • Adding extra symbols like & before curly braces
2. Which JSX snippet is syntactically correct?
easy
A. <p>Hello<p>
B. <div><img src='logo.png'></div>
C. <input type='text' />
D. <br>

Solution

  1. Step 1: Check self-closing tags in JSX

    In JSX, tags like input, img, and br must be self-closed with a slash /.
  2. Step 2: Validate each option

    <input type='text' /> correctly self-closes input. <div><img src='logo.png'></div> misses self-closing on img. <p>Hello<p> has an unclosed p tag. <br> misses self-closing on br.
  3. Final Answer:

    <input type='text' /> -> Option C
  4. Quick Check:

    Self-close void tags with / = <input type='text' /> [OK]
Hint: Always self-close void tags like in JSX [OK]
Common Mistakes:
  • Forgetting the slash on self-closing tags
  • Leaving tags unclosed
  • Using HTML void tags without self-closing
3. What will this JSX code render?
const name = 'Alice';
const element = <div>Hello, {name.toUpperCase()}!</div>;
medium
A. <div>Hello, Alice!</div>
B. <div>Hello, ALICE!</div>
C. <div>Hello, {name.toUpperCase()}!</div>
D. SyntaxError

Solution

  1. Step 1: Evaluate the JavaScript expression inside JSX

    The expression {name.toUpperCase()} calls the toUpperCase() method on the string 'Alice', resulting in 'ALICE'.
  2. Step 2: Understand JSX rendering

    JSX replaces the expression with its evaluated result, so the rendered output is <div>Hello, ALICE!</div>.
  3. Final Answer:

    <div>Hello, ALICE!</div> -> Option B
  4. Quick Check:

    JS expression evaluated = <div>Hello, ALICE!</div> [OK]
Hint: JSX renders evaluated expressions, not raw code [OK]
Common Mistakes:
  • Expecting JSX to show raw JavaScript code
  • Confusing string methods inside JSX
  • Thinking JSX does not evaluate expressions
4. Identify the error in this JSX code:
const element = <div>
  <p>Welcome</p>
  <p>User</p>
</div>;

const wrongElement = <p>Hello</p>
<p>World</p>;
medium
A. JSX expressions cannot contain multiple <p> tags
B. JSX requires all tags to be self-closing
C. JSX does not allow text inside <p> tags
D. JSX must have only one parent element per block

Solution

  1. Step 1: Analyze the JSX structure

    The first element uses a single parent div wrapping two p tags, which is correct. The second snippet has two sibling p tags without a parent wrapper.
  2. 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.
  3. Final Answer:

    JSX must have only one parent element per block -> Option D
  4. Quick Check:

    One parent element rule = JSX must have only one parent element per block [OK]
Hint: Wrap multiple JSX elements in one parent tag [OK]
Common Mistakes:
  • Leaving sibling JSX elements unwrapped
  • Thinking multiple root elements are allowed
  • Confusing self-closing tags with parent wrapping
5. You want to render a list of names inside a JSX component. Which snippet correctly uses JSX syntax to do this?
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>
hard
A. Correctly maps names to list items with JSX expressions
B. Uses forEach which returns undefined, so no list items render
C. Does not embed the variable, so 'name' is rendered literally
D. Maps names but transforms them to uppercase before rendering

Solution

  1. Step 1: Understand how to render lists in JSX

    Use map to transform array items into JSX elements. Each item must be embedded inside curly braces.
  2. Step 2: Evaluate each option

    The snippet {names.map(name => <li>{name}</li>)} correctly uses map and embeds {name}. The forEach snippet returns undefined, so no list items render. The snippet <li>name</li> renders the literal string 'name' because it lacks curly braces. The toUpperCase() snippet uses valid JSX syntax but transforms names to uppercase, which is not the original list.
  3. Final Answer:

    Correctly maps names to list items with JSX expressions -> Option A
  4. Quick Check:

    Use map with curly braces for list rendering = Correctly maps names to list items with JSX expressions [OK]
Hint: Use map with curly braces to render lists in JSX [OK]
Common Mistakes:
  • Using forEach instead of map for rendering
  • Forgetting curly braces around variables
  • Rendering variable names as plain text
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard