Write valid nested JSX
Write your first nested JSX tree
JSX is an extension of JavaScript syntax for describing UI. In a React project, the build tool converts JSX into JavaScript calls that produce React elements. JSX is not an HTML string, and writing JSX alone does not insert anything into the browser. The following example replaces src/App.jsx in the reviewed client app from Chapter 3. All correct examples in this chapter use that same entry module, with the modern automatic JSX transform enabled. Visible results are predictions until an approved React runtime is available.
export default function App() { return ( <main> <h1>Reading corner</h1> <p>One book at a time.</p> <hr /> </main> );}The expected page has a main area, a heading, a paragraph and a horizontal rule. Indentation makes parent and child relationships easy to see. The main element wraps the other elements; the return statement returns one JSX expression. The horizontal rule has no content and is written with a closing slash.
Close tags and return one expression
Opening tags with children need matching closing tags. Empty tags use the self-closing form, such as <hr /> or <input />. Adjacent top-level JSX tags cannot be written as one ungrouped expression. A semantic parent such as main or section can group them; the next lesson introduces fragments when no extra DOM parent is needed. This is a rule of JSX syntax, not a rule that a React component can only return one DOM node.
export default function App() { return (<h1>Books</h1><p>Three available</p>);}This fails at transformation time. Group the heading and paragraph inside a main element as in the first example, or use a fragment. Do not copy the invalid sample into a working app.
export default function App() { return <main><p>Books</main>;}The p tag must close before main closes. Change the return to <main><p>Books</p></main>.
Distinguish a DOM tag from a component
function ShelfLabel() { return <h2>Borrowed books</h2>;}
export default function App() { return ( <section> <ShelfLabel /> <p>Return books on Friday.</p> </section> );}The lowercase section, h2 and p names identify built-in browser elements. The capitalized ShelfLabel name refers to the JavaScript function defined above App. React renders the result of that component in the tree. Expected text: Borrowed books, followed by Return books on Friday. A lowercase custom function name used as a JSX tag does not select that function.
Understand the transform
With the automatic transform, a compiler inserts imports from react/jsx-runtime (or the development counterpart). Older classic configurations produce React.createElement calls and generally need React in scope. These are compiler settings, not two different JSX languages. Do not manually import internal JSX helpers for ordinary components. Local TypeScript transformation checks accompany this chapter; they do not prove React rendering or Vite integration.
Practice
Repair a malformed library notice and explain why it fails. Then distinguish the component name from its resulting heading. The next lesson adds JavaScript values and fragments.