Chapter 2 ยท Describing the UI

Components & JSX

The building blocks of every React app, and the HTML-like language you use to write them.

You will learn
  • What a component is and how to write one
  • How to nest components and split them into files
  • Default vs named import/export
  • JSX and its rules
  • Using JavaScript inside JSX with curly braces

Your first component

A component is a JavaScript function that returns markup. That's all it is.

Reading it line by line:

  1. function Profile() defines a normal JavaScript function. Its name must start with a capital letter.
  2. return ( ... ) returns markup. When the markup spans several lines, wrap it in parentheses. Without them, anything on the lines after return is ignored.
  3. <Profile /> uses the component, like a custom HTML tag. Here it's used three times, so you get three images.
  4. export default makes Gallery the main thing this file shares with other files.
Why the capital letter? That's how React tells your components apart from HTML tags. <section> (lowercase) is an HTML tag. <Profile /> (capitalised) is your component. If you name a component profile, React treats it as an unknown HTML tag and shows nothing useful.

Nesting: components inside components

Gallery renders Profile, so Gallery is the parent and each Profile is a child. Whole apps are trees of components nested like this:

โŒ Never define a component inside another component Always define components at the top level of the file, side by side.

Importing and exporting components

As your app grows, you split components into separate files. Each file is private by default. Use export to share something, and import to use it in another file.

Default export: one main thing per file

Named export: many things per file

SyntaxExport statementImport statement
Defaultexport default function Button() {}import Button from './Button';
Namedexport function Button() {}import { Button } from './Button';
A simple convention One component per file, file named after the component (Profile.jsx โ†’ Profile), with a default export. Files that contain JSX should end in .jsx.
Common import errors and what they mean
  • "Failed to resolve import './components/Profile'" โ†’ the path is wrong: a typo, the wrong folder, or a capital-letter mismatch (profile โ‰  Profile on Linux servers).
  • "Profile is not defined" โ†’ you forgot the import line.
  • "does not provide an export named 'default'" โ†’ the file uses a named export, so import it with braces.
  • "Unexpected JSX expression" โ†’ there's JSX in a .js file. Rename it to .jsx.

JSX: markup inside JavaScript

The HTML-like code you return is called JSX. It looks like HTML but it's really JavaScript: your build tool turns each tag into a function call. In the past, web pages kept HTML, CSS and JavaScript in separate files. React keeps a component's markup and logic together, because they change together.

The three rules of JSX

Rule 1: return a single root element. A function can only return one thing, so wrap siblings in a parent:

Rule 2: close all tags. Self-closing tags need a slash: <img />, <br />, <input />. Wrapping tags need their closing tag: <li>...</li>.

Rule 3: use camelCase for most attributes. JSX attributes become JavaScript object keys, and some HTML words are reserved in JavaScript:

HTMLJSXWhy
classclassNameclass is a JavaScript keyword
forhtmlForfor is a JavaScript keyword
onclick, tabindexonClick, tabIndexcamelCase
stroke-widthstrokeWidthno dashes in JavaScript names
<!-- comment -->{/* comment */}a JavaScript comment inside braces

Exceptions: aria-* and data-* attributes keep their dashes (aria-label, data-id).

Converting existing HTML Pasting a big chunk of HTML? Fix it with the three rules above: one parent, closed tags, and className / camelCase. Online "HTML to JSX" converters can do it automatically.

JavaScript in JSX with curly braces

Quotes give you plain text. Curly braces { } open a window into JavaScript: whatever is inside is evaluated, and the result goes into the markup.

Where you can use curly braces

Anything that produces a value works: variables, maths ({2 + 2}), function calls ({formatDate(today)}), ternaries. Statements such as if, for or const don't work inside braces. Do those above the return.

"Double curlies": objects in JSX

To pass a JavaScript object, wrap it in braces. The outer pair means "JavaScript here", and the inner pair is the object itself. You'll see this mostly with inline style:

Style property names are camelCase (backgroundColor, not background-color), and a plain number means pixels (marginTop: 8 โ†’ 8px). For most styling, prefer className with a CSS file.

Grouping data in an object

More examples

Example 1: composing a page from small components

Each component is small and easy to read. App reads like a table of contents for the page.

Example 2: converting HTML to JSX

The changes: a fragment around everything, {/* */} comments, htmlFor, className, camelCase (tabIndex, strokeWidth), and every tag closed.

Example 3: a dynamic className

Because className takes any JavaScript expression in braces, you can switch CSS classes with a ternary or a template string: className={`note ${isImportant ? 'important' : ''}`}.

Example 4: calling functions inside JSX

Recap

  • A component is a capitalised function that returns JSX. Define components at the top level and nest them freely.
  • Use export default / import X from for one main thing per file, and export / import { X } from for several.
  • JSX rules: one root (use <>), close every tag, camelCase attributes (className).
  • { } puts any JavaScript expression into text or attributes. {{ }} is just an object inside braces.

Exercises

1. Fix the component

This code has four problems. Find and fix them.

Show solution

2. Split into files

Move Profile into src/components/Profile.jsx with a default export, then import and render it three times from App.jsx.

Show solution

3. Use curly braces

Create a person object with name, imageUrl and theme. Show the name in an <h1>, the image with the name as its alt, and apply theme as the style of a wrapper <div>.

Show solution

4. Convert this HTML ๐ŸŸข easy

Show solution

5. Fix the two siblings ๐ŸŸข easy

Show solution

6. Today's date ๐ŸŸข easy

Show "Today is Thursday, 30 October 2026" (with the real date) using toLocaleDateString with options { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }.

Show solution

7. Expressions in braces ๐ŸŸข easy

Given const fruits = ['apple', 'banana', 'cherry'];, show: "You have 3 fruits. The first is APPLE. Total letters: 17."

Show solution

8. Inline styles from a variable ๐ŸŸก medium

Create const brand = { color: 'white', bg: '#087ea4' }. Make a <button> that uses those colours, with 8px vertical and 16px horizontal padding and rounded corners.

Show solution

9. Named exports ๐ŸŸก medium

Create src/components/Buttons.jsx with two named exports, PrimaryButton and DangerButton. Import both in App.jsx on one line and render them.

Show solution

10. A profile page from three files ๐Ÿ”ด hard

Build a profile page from three components in three files: Avatar.jsx (a round image), Bio.jsx (name and description, read from a person object defined in the same file), and ProfilePage.jsx (puts them side by side with flexbox). Render ProfilePage from App.

Show solution