Lesson 2 of 98:46 video

React components and JSX

Paste working HTML into a React component and it fails three times, then a fourth mistake makes part of your page disappear with no error at all. One small article card, built from an empty folder with Vite, shows why: every JSX tag becomes a JavaScript function call that returns an object. From that one picture you can work out the rules instead of memorizing them: one parent and fragments, closing every tag, className, curly braces, and why components need a capital letter.

In this lesson

  1. 0:00 Why React rejects your HTML
  2. 0:52 From an empty folder
  3. 1:44 Three complaints
  4. 2:48 What JSX turns into
  5. 4:04 Curly braces
  6. 5:04 Components inside components
  7. 5:55 The fourth mistake
  8. 7:14 Without JSX
  9. 7:38 It was never HTML

Here’s an article card written in plain HTML. Every browser shows it without complaint:

<h1>Reading list</h1>
<article class="card">
  <img src="/cover.svg" alt="Laptop on a desk">
  <h2>How Browsers Draw a Page</h2>
  <p class="author">Ana Ruiz · 6 min read</p>
  <button>Save</button>
</article>

Paste it into a React component and it fails three times in a row. Then a fourth mistake makes part of the page disappear with no error at all. All four come from one cause: JSX, the markup you write in React, only looks like HTML. This lesson builds the card from an empty folder, makes all four mistakes on purpose, and looks at what JSX turns into before the browser runs it. Once you’ve seen that, you can work out JSX’s rules instead of memorizing them.

From an empty folder

You need Node, which comes with npm. In an empty folder, run:

npm create vite@latest

Give the project a name, then choose React and JavaScript. Vite is the build tool; what it does with your markup comes up in a moment. Install the packages and start the dev server:

npm install
npm run dev

Three files make the page. index.html holds one empty div and loads main.jsx:

<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>

src/main.jsx tells React DOM to put something named App inside that div. React DOM is the package that turns React’s output into elements in the browser:

import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>,
)

And src/App.jsx is yours to write. App is a component: a function that returns a piece of the page. Replace the file with one that returns a heading:

export default function App() {
  return <h1>Reading list</h1>
}

Save it and “Reading list” is on the page. Notice that nothing calls App(). main.jsx only used App as a tag, <App />, and React called the function. Keep that in mind for the end.

Three complaints

Paste the card’s HTML under the heading:

export default function App() {
  return (
    <h1>Reading list</h1>
    <article class="card">
      <img src="/cover.svg" alt="Laptop on a desk">
      <h2>How Browsers Draw a Page</h2>
      <p class="author">Ana Ruiz · 6 min read</p>
      <button>Save</button>
    </article>
  )
}

An error covers the page:

[PARSE_ERROR] Adjacent JSX elements must be wrapped in an enclosing tag.

The heading and the card are two tags with nothing around them. The fix is a fragment, an empty pair of tags, <> and </>, that groups them and adds nothing to the page. Wrap both in one, and a different error takes its place:

[PARSE_ERROR] Expected corresponding JSX closing tag for 'img'.

In HTML an img tag never closes. In JSX it must, so end it with a slash: <img src="/cover.svg" alt="Laptop on a desk" />. Now the page loads. But the console shows a warning from React:

Invalid DOM property `class`. Did you mean `className`?

The card still works. Rename both class attributes to className and the console is clean:

export default function App() {
  return (
    <>
      <h1>Reading list</h1>
      <article className="card">
        <img src="/cover.svg" alt="Laptop on a desk" />
        <h2>How Browsers Draw a Page</h2>
        <p className="author">Ana Ruiz · 6 min read</p>
        <button>Save</button>
      </article>
    </>
  )
}

That’s three changes, and they look like three separate rules to learn. They aren’t. Look at who complained: the two errors didn’t come from the browser or from React. They came from Vite.

What JSX turns into

No browser can run JSX. Before your code reaches the browser, Vite rewrites every tag as an ordinary JavaScript function call. This heading:

<h2>How Browsers Draw a Page</h2>

becomes a call to a function named jsx, imported from React. Its two arguments are the tag’s name as a string, and an object holding everything else:

import { jsx } from "react/jsx-runtime";

jsx("h2", { children: "How Browsers Draw a Page" });

Run that call and you don’t get an element on the page. You get a small object that describes one. To see it, save this as jsx.mjs in the project and run node jsx.mjs:

import { jsx } from "react/jsx-runtime";

const heading = jsx("h2", { children: "How Browsers Draw a Page" });
console.log(heading.type); // h2
console.log(heading.props); // { children: 'How Browsers Draw a Page' }

So JSX was never HTML. It’s a short way to write function calls that return objects, and React builds the page from those objects. Now read the three complaints again:

  • One parent. Two tags side by side become two calls, and a function can return only one value. A fragment turns them into one call, whose children are the other two.
  • Every tag closes. Vite isn’t reading HTML, so it has no list of tags that never close. An img with no closing slash leaves the tags unmatched.
  • className. This one was only a warning, because the rule is React’s own. React names attributes after the DOM’s JavaScript properties, in camelCase, and the DOM calls this one className, because JavaScript already uses the word class.

Curly braces

So far the card’s text is typed into the markup. Real pages get their text from data, so put the article in an object. Inside JSX, curly braces mean “this part is JavaScript”:

const article = {
  title: 'How Browsers Draw a Page',
  cover: '/cover.svg',
  author: 'Ana Ruiz',
  minutes: 6,
}

export default function App() {
  return (
    <>
      <h1>Reading list</h1>
      <article className="card">
        <img src={article.cover} alt="Laptop on a desk" />
        <h2>{article.title}</h2>
        <p className="author">
          {article.author} · {article.minutes} min read
          {article.minutes < 5 ? ' · Quick read' : ''}
        </p>
        <button>Save</button>
      </article>
    </>
  )
}

The cover goes into src in braces instead of quotes. Keep the quotes, as in src="{article.cover}", and the browser gets that literal text, braces included.

Change minutes to 4 and save. Vite sends the changed file to the browser, and the card reads “Ana Ruiz · 4 min read · Quick read”.

Each pair of braces ends up as one value inside the function call: <h2>{article.title}</h2> becomes jsx("h2", { children: article.title }). So braces accept anything that produces a value: a property, a calculation, or a ternary like the “Quick read” one. An if statement produces no value, so Vite rejects it:

{if (article.minutes < 5) { ' · Quick read' }}
// [PARSE_ERROR] Unexpected token

Components inside components

A reading list needs more than one card. You could copy the markup, but then every change has to be made twice. Instead, move the card’s markup into its own function, ArticleCard, and use its name as a tag inside App:

function ArticleCard() {
  return (
    <article className="card">
      <img src={article.cover} alt="Laptop on a desk" />
      <h2>{article.title}</h2>
      <p className="author">
        {article.author} · {article.minutes} min read
      </p>
      <button>Save</button>
    </article>
  )
}

export default function App() {
  return (
    <>
      <h1>Reading list</h1>
      <ArticleCard />
      <ArticleCard />
    </>
  )
}

That’s nesting: App returns a heading and two ArticleCards, and ArticleCard returns the article. Two tags, two cards, one function to edit. Both cards show the same article, because both run the same function on the same data. Giving each card its own data is what props are for.

The fourth mistake

One more component, for the author line: a small photo, the name and the reading time. Plain JavaScript functions usually start with a small letter, so call it author. Add avatar: '/avatar.svg' to the article object, then:

function author() {
  return (
    <p className="author">
      <img src={article.avatar} alt="" />
      {article.author} · {article.minutes} min read
    </p>
  )
}

function ArticleCard() {
  return (
    <article className="card">
      <img src={article.cover} alt="Laptop on a desk" />
      <h2>{article.title}</h2>
      <author />
      <button>Save</button>
    </article>
  )
}

Save, and there’s no error screen. But both author lines are gone. The only clue is one warning in the console:

The tag <author> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.

Vite turned the lowercase tag into a call with a string, jsx("author", {}). To React a string is the name of an HTML tag, so React DOM created an <author> element. Browsers accept tags they don’t know, so the page got an empty <author></author>, and your function never ran.

Rename it Author, with a capital A, and use <Author />. This time Vite passes the function itself: jsx(Author, {}). The capital letter is how the build tool tells your components from HTML tags.

The tag still doesn’t call Author. It makes an object that says “an Author goes here”. You can check that in jsx.mjs:

import { jsx } from "react/jsx-runtime";

let calls = 0;
function Author() {
  calls++;
  return jsx("p", { className: "author", children: "Ana Ruiz" });
}

const element = jsx(Author, {});
console.log(element.type === Author); // true
console.log(calls); // 0
console.log(jsx("author", {}).type); // author

React calls your function later, when it builds the page. It gets more objects back, and keeps going until only HTML tags are left. Then React DOM turns those into real elements. So there are three different things: the function you write, the description it returns, and the elements on the page.

Without JSX

The function isn’t always named jsx. Older projects turn the same tags into calls to React.createElement, which is why old code imports React at the top of every component file. You can write those calls by hand and skip JSX entirely:

import { createElement } from "react";

const heading = createElement("h2", null, "How Browsers Draw a Page");
console.log(heading.type); // h2
console.log(heading.props); // { children: 'How Browsers Draw a Page' }

Whichever function your tools write, the idea holds: tags go in, objects come out.

It was never HTML

Every tag in a component becomes a function call, and the call returns an object. Each rule follows from that:

  • A function returns one value, so a component returns one parent (or a fragment).
  • JSX has no list of tags that never close, so every tag closes.
  • Attributes use the DOM’s JavaScript names, so className.
  • Braces hold one value, so they take expressions and not statements.
  • A capital letter decides between your function and an HTML tag.

That’s also the answer when an interviewer asks what JSX is: syntax that a build tool turns into function calls. Those calls return descriptions, and React turns the descriptions into the page.

Video transcript

Why React rejects your HTML

React rejects HTML that every browser accepts. Take this card. It's plain HTML and it works. But paste it into a React file and an error covers the page. Fix that error and a second one appears. Fix that one and React still complains. That's because JSX only looks like HTML. JSX is the markup you write in React and by the end of this video you'll know what it really is. There's also a fourth mistake. It's worse because there's no error at all: change one letter and part of your page quietly disappears. All four have one cause. So we'll build this card from an empty folder, make all four mistakes on purpose, and look at what JSX turns into before the browser runs it. After that you won't have to memorize JSX's rules. You can work them out.

From an empty folder

So make the project. In an empty folder run npm create vite at latest and choose React and then JavaScript. Vite is the build tool and its real job comes up in a minute. Three files make the page. index dot html holds one empty div. main dot jsx tells React DOM to put something named App inside that div. React DOM is the package that draws React's output. And App dot jsx is ours to write. App is a component: a function that returns a piece of the page. Ours returns one heading that says Reading list. Save the file and the heading is on the page. Notice that we never called App. main dot jsx only used App as a tag and React called the function. Remember that for the end. Now bring in the card.

Three complaints

The card already exists as HTML, so paste it in under the heading. An error covers the page: "Adjacent JSX elements must be wrapped in an enclosing tag." The heading and the card are two tags with nothing around them, and that isn't allowed. The fix is a fragment: an empty pair of tags that groups them and adds nothing to the page. Add it, and a new error takes its place: "Expected corresponding JSX closing tag for img." In HTML an image tag never closes. Here it must, so close it with a slash. Now the page loads. But open the console, and React has a warning: "Invalid DOM property class. Did you mean className?" The card still works, but rename both attributes and the console is clean. Three changes, and they look like three separate rules. They aren't. Notice who complained. The two errors didn't come from the browser or from React. They came from Vite. So what is Vite doing with your markup?

What JSX turns into

No browser can run JSX. So before your code reaches the browser, Vite rewrites every tag as an ordinary JavaScript function call. This h2 becomes a call to a function named jsx, with two arguments: the tag's name as a string, and an object holding everything else. Now run that call. You don't get an element on the page. You get a small object that describes one: its type, h2, and its text. So JSX was never HTML. JSX is a short way to write function calls that return objects, and React builds the page from those objects. Now read the three complaints again. Two tags with nothing around them become two calls, and a function can return only one value. A fragment turns them into one call. The image tag: Vite isn't reading HTML, so it has no list of tags that never close. Every tag has to be closed, or the tags don't match. And class? React only warned, because this rule is React's own. React names attributes after the DOM's JavaScript properties, in camelCase, and the DOM calls this one className, because JavaScript already uses the word class.

Curly braces

So far the card's text is typed into the markup. Real pages get their text from data, so put the article in an object: a title, a cover image, an author, and the minutes it takes to read. Inside JSX, curly braces mean "this part is JavaScript". The title goes between the h2 tags, in braces. The author and the minutes go in the same way. The cover goes into the image's src, in braces instead of quotes. Keep the quotes and the browser gets the literal text, braces included. Now change six minutes to four in the object and save the file. Vite sends the changed file to the browser, and the card says four. Each pair of braces ends up as one value inside the function call. So braces accept anything that produces a value: a property, a calculation, a ternary that adds "Quick read" to short articles. An if statement produces no value, so Vite rejects it.

Components inside components

The card now reads from data. But a reading list needs more than one card. You could copy the markup, but then every change has to be made twice. So give the card a name. Move its markup into a new function, ArticleCard, and you have a second component. To use it, write its name as a tag inside App. That's nesting: App returns a heading and an ArticleCard, and ArticleCard returns the article. Need another card? Write the tag again. Two tags, two cards, one function to edit. Both cards show the same article for now, because both run the same function on the same data. Giving each card its own data is what props are for, and props are the next step in this course. One more component to go, and the fourth mistake is waiting in it.

The fourth mistake

The last component is for the author line: a small photo, the name, the reading time. Plain JavaScript functions start with a small letter. So name this one author, with a small a, and use it as a tag. Save. No error screen. But look at the cards: both author lines are gone. The only clue in the browser is one warning in the console. So what happened? Vite turned the lowercase tag into a call with a string, the text "author". To React, a string is the name of an HTML tag, so React DOM created an author element. Browsers accept tags they don't know, so the page got an empty author element, and your function never ran. Now give the name a capital A. This time Vite passes the function itself. So the capital letter is how the build tool tells your components from HTML tags. And the tag still doesn't call Author. It makes an object that says "an Author goes here". React calls your function when it builds the page, gets more objects back, and keeps going until only HTML tags are left. Then React DOM turns those into real elements. That's three different things: the function you write, the description it returns, and the elements on the page.

Without JSX

One caveat. The function isn't always named jsx. Older projects turn the same tags into calls to React's createElement and that's why old code imports React in every component file. You can even write those calls by hand and skip JSX. Whichever function your tools write, the idea holds: tags go in and objects come out.

It was never HTML

So who rejected your HTML? Not the browser and mostly not React. The build tool did, because what you pasted was never going to be HTML. Every tag in a component becomes a function call and the call returns an object. A function returns one value, so one parent. JSX has no list of tags that never close, so every tag closes. Attributes use the DOM's JavaScript names, so className. Braces hold one value. And a capital letter decides between your function and an HTML tag. That's also your answer when an interviewer asks what JSX is. It's syntax that a build tool turns into function calls. Those calls return descriptions and React turns the descriptions into the page. You started with an empty folder. Now you can explain every line in it. The next step in this React course is on screen now.

Learn more