LearnThatStack Ace your next interview
React · question
Question 6 of 103

What are the rules of JSX?

beginner
← All React questions
Re-explain

JSX needs a single parent element, camelCase attributes and closed tags, because it compiles to JavaScript rather than HTML. The key rules:

  1. A component must return a single parent element, or wrap its siblings in a React Fragment.
  2. Attributes use camelCase, so class becomes className and onclick becomes onClick.
  3. Every tag must close, and self-closing tags end with />.
  4. Curly braces embed JavaScript expressions, such as {variable} or {expression}.
  5. A boolean attribute can be written as disabled={true} or just disabled.
// Correct JSX
function MyComponent() {
  return (
    <div className="container">
      <img src="image.jpg" alt="Description" />
      <p>Count: {count}</p>
    </div>
  );
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • When would you use a Fragment instead of wrapping siblings in a div? A Fragment groups siblings without adding a DOM node, which matters in tables and flex layouts. Only the <Fragment key> form takes a key.
  • What does JSX compile to, and how does that explain these rules? It compiles to calls like React.createElement or jsx(). A function returns one value, and class is a reserved word in JavaScript.
  • How do you render something conditionally when braces only accept expressions? Use a ternary or &&, or compute the element in a variable before return. An if statement can't go inside braces.
  • What renders if you write {count && <List />} and count is 0? It renders 0. React skips false, null, undefined and true, but it renders numbers. Use count > 0 or a ternary instead.
  • Is it safe to put user input inside JSX braces? React escapes strings before it inserts them, which blocks most XSS. dangerouslySetInnerHTML skips that escaping.

What you can say

  1. JSX looks like HTML, but it compiles to JavaScript function calls, and almost every rule comes from that.
  2. A component has to return one parent element, so if I have siblings I wrap them in a div or a Fragment.
  3. Attributes are camelCase, so class becomes className, since class is a reserved word in JavaScript, and onclick becomes onClick.
  4. Every tag has to close, and a tag with no children closes itself with a slash, like <img />.
  5. Curly braces switch into JavaScript, so {count} or any other expression works, and a boolean prop like disabled can be written bare to mean true.
  6. The catch is that braces take expressions, not statements, so an if or a for loop has to become a ternary, && or map, or move above the return.

Weak answers to avoid

  • Recites the rules with no reason A list of className and /> sounds memorized. Tie each rule to JSX compiling to JavaScript, for example class being a reserved word.
  • Calls JSX HTML inside JavaScript JSX is syntax that compiles to function calls. Calling it HTML hides the reason its rules differ from HTML, which is what the interviewer wants to hear.
  • Says any JavaScript can go in braces Braces take expressions only. An if or for statement there is a syntax error, so use a ternary or &&, or compute the value before return.
  • Adds a wrapper div for every sibling group Extra divs clutter the DOM and can break table or flex layouts. Name Fragment as the way to return siblings without an extra node.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime