Chapter 2 ยท Describing the UI
Components & JSX
The building blocks of every React app, and the HTML-like language you use to write them.
- 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:
function Profile()defines a normal JavaScript function. Its name must start with a capital letter.return ( ... )returns markup. When the markup spans several lines, wrap it in parentheses. Without them, anything on the lines afterreturnis ignored.<Profile />uses the component, like a custom HTML tag. Here it's used three times, so you get three images.export defaultmakesGallerythe main thing this file shares with other files.
<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:
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
| Syntax | Export statement | Import statement |
|---|---|---|
| Default | export default function Button() {} | import Button from './Button'; |
| Named | export function Button() {} | import { Button } from './Button'; |
Profile.jsx โ Profile), with a default export. Files that contain JSX should end in .jsx.
- "Failed to resolve import './components/Profile'" โ the path is wrong: a typo, the wrong folder, or a capital-letter mismatch (
profileโProfileon Linux servers). - "Profile is not defined" โ you forgot the
importline. - "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
.jsfile. 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:
| HTML | JSX | Why |
|---|---|---|
class | className | class is a JavaScript keyword |
for | htmlFor | for is a JavaScript keyword |
onclick, tabindex | onClick, tabIndex | camelCase |
stroke-width | strokeWidth | no dashes in JavaScript names |
<!-- comment --> | {/* comment */} | a JavaScript comment inside braces |
Exceptions: aria-* and data-* attributes keep their dashes (aria-label, data-id).
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
- As text inside a tag:
<h1>{name}</h1>โ - As an attribute value, right after
=:src={avatar}โ - Not as a tag name:
<{tag}>โ
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 fromfor one main thing per file, andexport/import { X } fromfor 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
๐ Official docs: Your First Component ยท Importing and Exporting ยท Writing Markup with JSX ยท JavaScript in JSX