🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

JSX

AI & DATA SCIENCE // jsx-structure

JSX elements can nest children, embed expressions, and use fragments to group multiple elements — this reference covers JSX's core structural rules.

Syntax

<>
  <ChildOne />
  <ChildTwo />
</>

Deep Dive Course

Every JSX expression must have exactly one root/enclosing element — returning two sibling elements directly from a component is a compile error, which is exactly why React provides Fragments, written as <React.Fragment> or the shorthand <>...</>, to group multiple children together without introducing an actual extra wrapper element, like a div, into the rendered DOM. JSX also allows self-closing tags for elements with no children, <img />, requires every list of rendered elements to include a unique key prop for React's reconciliation algorithm to track items correctly across re-renders, and disallows JavaScript reserved words like class and for as attribute names, using className and htmlFor instead.

1Understanding JSX

Every JSX expression must have exactly one root/enclosing element — returning two sibling elements directly from a component is a compile error, which is exactly why React provides Fragments, written as <React.Fragment> or the shorthand <>...</>, to group multiple children together without introducing an actual extra wrapper element, like a div, into the rendered DOM. JSX also allows self-closing tags for elements with no children, <img />, requires every list of rendered elements to include a unique key prop for React's reconciliation algorithm to track items correctly across re-renders, and disallows JavaScript reserved words like class and for as attribute names, using className and htmlFor instead.

💡

Use a Fragment, <>...</>, instead of an unnecessary wrapper <div> purely to satisfy JSX's single-root-element rule — a Fragment groups elements without adding any extra node to the actual rendered DOM.

editor.html
function Layout() {
  return (
    <>
      <Header />
      <Main />
      <Footer />
    </>
  );
}
localhost:3000

2Practical Example

Here is a real-world application of JSX showing how it is used in production React code.

editor.html
function List({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.text}</li>)}
    </ul>
  );
}

// <List items={[{ id: 1, text: 'A' }, { id: 2, text: 'B' }]} />
localhost:3000

3Best Practices

Follow these guidelines when working with JSX:

1. Wrap multiple sibling elements in a Fragment, <>...</>, rather than an unnecessary wrapper div, when a component needs to return more than one top-level element

2. Always provide a unique, stable key prop on elements rendered from a list via .map(), rather than using the array index when the list can reorder

3. Use className and htmlFor instead of the reserved JavaScript words class and for when setting those specific HTML attributes in JSX

⚠️

Tip: Use a Fragment, <>...</>, instead of an unnecessary wrapper <div> purely to satisfy JSX's single-root-element rule — a Fragment groups elements without adding any extra node to the actual rendered DOM.

editor.html
function Layout() {
  return (
    <>
      <Header />
      <Main />
      <Footer />
    </>
  );
}
localhost:3000

4Self-Closing Tags Are Required

Unlike HTML, JSX requires every tag to be explicitly closed. Elements that never have children — img, br, hr, input — are self-closing in HTML but must still carry the trailing slash in JSX: <img src={url} />, not <img src={url}>. Forgetting it is a compile error.

editor.html
// Compile error
<img src={url}>

// Required
<img src={url} />
localhost:3000

5Why the Array Index Is a Risky key

React matches list elements across re-renders by their key, preserving each item's state and updating only what actually changed. The array index works fine for a static list but breaks once items can be reordered, inserted, or removed — the index shifts to a different item, and React can attach previous state (like a focused input's typed value) to the wrong row.

⚠️

Only use the array index as a key when the list is static and never reorders, filters, or has items inserted/removed — otherwise, key by a stable, unique field from the data itself.

editor.html
// Breaks if the list reorders
items.map((item, index) => <li key={index}>{item.text}</li>)

// Stable across reorders
items.map(item => <li key={item.id}>{item.text}</li>)
localhost:3000

Examples

Example 01Basic Usage
function Layout() {
  return (
    <>
      <Header />
      <Main />
      <Footer />
    </>
  );
}
Example 02Advanced Example
function List({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.text}</li>)}
    </ul>
  );
}

// <List items={[{ id: 1, text: 'A' }, { id: 2, text: 'B' }]} />

Best Practices

  • Wrap multiple sibling elements in a Fragment, <>..., rather than an unnecessary wrapper div, when a component needs to return more than one top-level element
  • Always provide a unique, stable key prop on elements rendered from a list via .map(), rather than using the array index when the list can reorder
  • Use className and htmlFor instead of the reserved JavaScript words class and for when setting those specific HTML attributes in JSX
  • Always close every tag explicitly, including otherwise-childless elements like img and br, with a trailing slash
  • Key list items by a stable, unique field from the data (like a database id), not the array index, whenever the list can reorder or change length

Interview Question

Why does React require a unique key prop on elements rendered from a list, and why can using the array index as that key cause bugs when the list can be reordered?

Hint: Think about how React's reconciliation algorithm uses the key to match elements between the previous and new render.

React's reconciliation algorithm uses each element's key to match up individual elements between the previous render and the new one, letting it correctly determine which elements were added, removed, or simply reordered, rather than assuming every element at a given position is automatically the same one as before. Using the array index as the key ties an element's identity to its position rather than to the actual underlying data it represents — if the list is reordered, filtered, or has an item inserted or removed from the middle, elements that logically represent different underlying data can end up reusing the same index-based key as some previous element that was at that position, causing React to incorrectly preserve state or DOM nodes that belonged to a completely different item. Using a genuinely stable, unique identifier from the data itself, like a database id, keeps each element's identity correctly tied to the actual data it represents regardless of how the list's order or contents change.

Exercises

MediumPractice using JSX in a real scenario.
View Solution
function Layout() {
  return (
    <>
      <Header />
      <Main />
      <Footer />
    </>
  );
}

Frequently Asked Questions

Why does React require a unique key prop on elements rendered from a list, and why can using the array index as that key cause bugs when the list can be reordered?

React's reconciliation algorithm uses each element's key to match up individual elements between the previous render and the new one, letting it correctly determine which elements were added, removed, or simply reordered, rather than assuming every element at a given position is automatically the same one as before. Using the array index as the key ties an element's identity to its position rather than to the actual underlying data it represents — if the list is reordered, filtered, or has an item inserted or removed from the middle, elements that logically represent different underlying data can end up reusing the same index-based key as some previous element that was at that position, causing React to incorrectly preserve state or DOM nodes that belonged to a completely different item. Using a genuinely stable, unique identifier from the data itself, like a database id, keeps each element's identity correctly tied to the actual data it represents regardless of how the list's order or contents change.

Related Functions

jsxcomponent-renderingchildren