0
0
Reactframework~15 mins

JSX syntax rules in React - Deep Dive

Choose your learning style9 modes available
Overview - JSX syntax rules
What is it?
JSX is a special syntax used in React to write HTML-like code inside JavaScript. It lets you describe what the user interface should look like in a way that feels natural and easy to read. JSX is not a string or HTML but a syntax that compiles to JavaScript function calls. It helps combine structure and logic in one place for building web components.
Why it matters
Without JSX, writing user interfaces in React would be much harder and less intuitive because you'd have to use plain JavaScript function calls to create elements. JSX makes the code look like HTML, which is familiar to most developers, speeding up development and reducing mistakes. It also helps React understand the structure of your UI clearly, improving performance and maintainability.
Where it fits
Before learning JSX syntax rules, you should understand basic JavaScript and React concepts like components and functions. After mastering JSX, you can learn about React hooks, component state, and advanced rendering patterns to build dynamic applications.
Mental Model
Core Idea
JSX lets you write HTML-like code inside JavaScript that React transforms into UI elements.
Think of it like...
JSX is like writing a recipe in your native language that a chef (React) understands and turns into a delicious dish (the UI).
JavaScript Code
  └─ JSX Syntax (looks like HTML)
       └─ Compiled by React
            └─ Creates UI Elements

JSX Example:
<div>
  <h1>Hello</h1>
</div>

Becomes:
React.createElement('div', null, React.createElement('h1', null, 'Hello'))
Build-Up - 7 Steps
1
FoundationJSX looks like HTML inside JavaScript
🤔
Concept: JSX syntax allows writing tags that look like HTML directly in JavaScript code.
In JSX, you write tags like
,

, or

Result
The function returns a JSX element that React can render as a heading on the page.
Understanding that JSX is not a string or HTML but a special syntax inside JavaScript helps you see how React builds the UI.
2
FoundationJSX must return a single root element
🤔
Concept: JSX expressions must have one parent element wrapping all other elements.
If you want to return multiple elements, you must wrap them in one container element like a
or React.Fragment: function List() { return (
  • Item 1
  • Item 2
  • ); } Or using a fragment: function List() { return ( <>
  • Item 1
  • Item 2
  • ); }
    Result
    React can only render one root element, so wrapping multiple elements prevents errors.
    Knowing the single root rule avoids common syntax errors and helps structure your components properly.
    3
    IntermediateJSX uses camelCase for attributes
    🤔Before reading on: do you think JSX attributes use HTML names like 'class' or JavaScript names like 'className'? Commit to your answer.
    Concept: JSX attributes mostly use camelCase naming instead of HTML attribute names.
    For example, instead of 'class' you write 'className', and instead of 'onclick' you write 'onClick'. This matches JavaScript naming conventions:
    Result
    JSX attributes map to JavaScript properties, making event handling and styling consistent.
    Understanding camelCase attribute names prevents bugs and confusion when adding styles or events.
    4
    IntermediateJSX expressions use curly braces for JavaScript
    🤔Before reading on: do you think you can put any JavaScript code directly inside JSX tags or only inside curly braces? Commit to your answer.
    Concept: To include JavaScript values or expressions inside JSX, you wrap them in curly braces {}.
    For example: const name = 'Alice'; return

    Hello, {name}!

    ; You can also use expressions: return

    {1 + 2}

    ; But you cannot put statements like if or for directly inside JSX without wrapping them in JavaScript code.
    Result
    JSX renders dynamic content by evaluating expressions inside curly braces.
    Knowing when and how to use curly braces lets you mix JavaScript logic with UI markup cleanly.
    5
    IntermediateJSX elements must be properly closed
    🤔
    Concept: All JSX tags must be closed, either with a closing tag or self-closing slash.
    Unlike HTML, JSX requires every tag to be closed: Correct:
    Incorrect:
    This rule helps JSX parse the code correctly.
    Result
    Properly closed tags prevent syntax errors and parsing issues.
    Recognizing JSX's strict closing rules avoids frustrating errors during compilation.
    6
    AdvancedJSX compiles to React.createElement calls
    🤔Before reading on: do you think JSX runs directly in the browser or is transformed before running? Commit to your answer.
    Concept: JSX is not understood by browsers directly; it is compiled into React.createElement calls by tools like Babel.
    For example, this JSX:
    Hello
    Becomes: React.createElement('div', null, 'Hello') This function call creates a React element object that React uses to build the UI.
    Result
    JSX is a developer-friendly syntax that compiles into JavaScript function calls React understands.
    Knowing JSX compiles to function calls clarifies why JSX must follow strict syntax rules.
    7
    ExpertJSX supports embedding expressions but not statements
    🤔Before reading on: can you write an if statement directly inside JSX tags or only expressions? Commit to your answer.
    Concept: JSX allows embedding JavaScript expressions inside curly braces but not statements like if or loops directly.