Skip to content
KALANBLON
Show contents

Chapter 1 of 6 · 20 min

Components, JSX and props

By the end of this chapter, you'll be able to

  • Explain what a component is and why an interface is split up
  • Write JSX and understand what it becomes
  • Pass properties (props) to a component
  • Display a list with map and a unique key

An interface made of components

A React component is a function that receives data and returns a description of the interface. A page is split into small reusable components, assembled like bricks:

<Catalogue>
  ├── <SearchBar />
  └── <CourseList>
        ├── <CourseCard title="Python: your first steps" level="Beginner" />
        ├── <CourseCard title="SQL: your first queries" level="Beginner" />
        └── <CourseCard title="IP networks" level="Intermediate" />

The same CourseCard component is reused three times, with different data.

JSX

Here is the CourseCard component. The syntax that looks like HTML in the middle of JavaScript is called JSX:

function CourseCard({ title, level, minutes }) {
  return (
    <article className="card">
      <h3>{title}</h3>
      <p>{level} · {minutes} min</p>
    </article>
  );
}

Three rules to remember:

  • braces { } insert a JavaScript expression: {title}, {minutes / 60}…;
  • you write className and not class (a reserved word in JavaScript);
  • a component returns a single root element (here <article>), which can contain everything else.

The browser does not understand JSX. A build tool (Vite, which we will use) turns it into ordinary JavaScript function calls. To understand what it becomes, let's write a mini version of this transformation ourselves:

function h(type, props, ...children) {
  return { type, props: props ?? {}, children };
}

function CourseCard({ title, level, minutes }) {
  return h("article", { className: "card" },
    h("h3", null, title),
    h("p", null, `${level} · ${minutes} min`));
}

const element = CourseCard({ title: "Python: your first steps", level: "Beginner", minutes: 95 });
console.log(element.type, element.props.className);
console.log(element.children[0].children[0], "|", element.children[1].children[0]);
article card
Python: your first steps | Beginner · 95 min

A component therefore returns a simple object describing the interface. React compares this object with the previous one and only changes in the page what has changed.

It only touches in the page what really changed: fast and without flicker.

Step 5 of 5
Text description of the animation
  1. The component receives its data as properties.
  2. React calls the component's function.
  3. The function returns a description of the interface: a tree of objects.
  4. If the data changes, React compares the new description with the old one.
  5. It only touches in the page what really changed: fast and without flicker.

Properties (props)

Properties (props) are the data a parent component passes to a child component, like HTML attributes. The component receives them in an object, which is often destructured directly in the parameters: function CourseCard({ title, level, minutes }). They are read-only: a component never modifies its own props.

Displaying a list

To display a list, an array of data is turned into an array of elements with map. Each element gets a unique, stable key (key), which lets React know which card is which when the list changes:

function CourseList({ courses }) {
  return (
    <section>
      {courses.map((c) => (
        <CourseCard key={c.slug} title={c.title} level={c.level} minutes={c.minutes} />
      ))}
    </section>
  );
}

The list's logic, on the other hand, can be tested without React, with ordinary JavaScript:

const courses = [
  { slug: "python-first-steps", title: "Python: your first steps", level: "Beginner", minutes: 95 },
  { slug: "sql-first-steps", title: "SQL: your first queries", level: "Beginner", minutes: 80 },
  { slug: "ip-networks", title: "IP networks", level: "Intermediate", minutes: 125 },
];
const keys = courses.map((c) => c.slug);
console.log(keys.length === new Set(keys).size ? "unique keys" : "duplicate keys");
console.log(courses.filter((c) => c.level === "Beginner").map((c) => c.title));
unique keys
["Python: your first steps","SQL: your first queries"]

Mini-lab: a catalogue's logicJavaScript

Output

Click “Run” to execute your code.

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: a badge component

Write a LevelBadge component that receives a level prop and displays a <span> with the level's text and a CSS class that depends on the level: badge beginner, badge intermediate or badge advanced. First test the function that computes the class in plain JavaScript.

Hint 1
A lookup object { "Beginner": "beginner", … } avoids a long chain of if.
Hint 2
In JSX, a computed class is written className={cls} or className={`badge ${suffix}`}.
Show the solution and common mistakes
const SUFFIXES = { "Beginner": "beginner", "Intermediate": "intermediate", "Advanced": "advanced" };

function badgeClass(level) {
  return `badge ${SUFFIXES[level] ?? "unknown"}`;
}

console.log(badgeClass("Beginner"));
console.log(badgeClass("Advanced"));
console.log(badgeClass("Expert"));
badge beginner
badge advanced
badge unknown
function LevelBadge({ level }) {
  return <span className={badgeClass(level)}>{level}</span>;
}

The logic (computing the class) is separated from the rendering: it is easy to test, and the component stays short.

Common mistakes:

  • Writing class= instead of className=: React shows a warning and the class may not apply.
  • Forgetting the case of an unknown level: without the default ?? "unknown", the class becomes badge undefined.

Summary

  1. 1A React component is a function that receives props and returns a description of the interface.
  2. 2JSX mixes tags and JavaScript in braces; it is turned into function calls.
  3. 3React compares the new description with the old one and only updates what changed.
  4. 4Props are read-only, passed from parent to child.
  5. 5A list is displayed with map, and each element gets a unique, stable key taken from the data.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.