Intermediate · Building the frontend · topic 13 of 34 · 30 min
Components and props: building the notes list
Explanation
What React does for you
React is a JavaScript library for building user interfaces (UI: what the user sees and taps). You describe what the screen should look like for the current data, and React updates the real page (the DOM) to match. You do not write code like "find this div and change its text" by hand.
You can start a React 19 app with Vite (a fast build tool: npm create vite@latest notes-web -- --template react) or inside Next.js, which adds routing and server features. The components you write look the same in both.
A component is a function that returns UI
A component is a JavaScript function whose name starts with a capital letter and which returns JSX. JSX is HTML-like syntax inside JavaScript. A build tool turns it into plain JavaScript calls before the browser runs it. Inside JSX, curly braces { } mean "run this JavaScript and show the result".
function NoteCard({ note, onDelete }) {
return (
<article className="note-card">
<h3>{note.title}</h3>
<p>{note.body}</p>
<button type="button" onClick={() => onDelete(note.id)}>
Delete
</button>
</article>
);
}Props: the inputs of a component
Props (short for properties) are the values a parent component passes to a child, written like HTML attributes: <NoteCard note={note} onDelete={removeNote} />. The child reads them from its first argument. Props are read-only: a child must never change them. If something must change, the parent passes a function (like onDelete) and the child calls it.
Composition: small pieces inside bigger ones
Composition means building a big component out of small ones. Our notes page is a tree: NotesPage contains a SearchBar and a NoteList; NoteList contains one NoteCard per note. Each piece is easy to read, test and reuse.
NotesPage
SearchBar
NoteList
NoteCard (one per note)
NewNoteButtonRendering a list with map and key
To show many notes, call notes.map() and return one NoteCard per note. React also needs a key on each item: a value that is unique among the siblings and stays the same for that item over time. React uses the key to know which card is which when the list changes.
function NoteList({ notes, onDelete }) {
if (notes.length === 0) return <p>No notes yet.</p>;
return (
<ul className="note-list">
{notes.map((note) => (
<li key={note.id}>
<NoteCard note={note} onDelete={onDelete} />
</li>
))}
</ul>
);
}React escapes text for you
When you write {note.title}, React treats the value as plain text. If a user saves a title like <img src=x onerror=alert(1)>, React shows those characters on screen instead of running them. This protects you from XSS (cross-site scripting: an attacker getting their script to run in other users' browsers).
Watch the lesson
Components and props: building the notes list · 1/6
In this module we build the frontend of our Notes app with React 19. We start with components.
Prefer a video by a teacher? Find free videos on “react components props rendering lists key tutorial” on YouTube ↗
In real life: A food delivery app's restaurant list
The home screen of a food delivery app is built from components: a SearchBar at the top, a row of CategoryChip components (Biryani, Pizza, Dosa), and a RestaurantList made of many RestaurantCard components. Each card gets props such as name, rating, delivery time and photo. The same RestaurantCard is reused on the search results page and the favourites page. Each card is keyed by the restaurant id, so when the list re-sorts by delivery time, React moves the cards instead of mixing up their details.
Practice (0/3 done)
NoteCard shows a short preview of the body. Write previewText(body, max) that returns the FIRST LINE of body (text before the first "\n"), trimmed. If that line is longer than max characters, cut it to max characters and add "..." at the end. If body is missing or empty, return "".
Before NoteList maps the notes, they must be in display order. Write sortForDisplay(notes) that returns a NEW array: pinned notes first, then within each group the newest updated_at first. updated_at is text like "2026-02-04 09:00", so comparing the strings works. Do not change the input array.
Write the HTML that NoteList should produce (plain HTML, no React), so you know the target markup. Include: an <h2> with the text "My notes"; a <ul> with at least two <li> items; in each <li>, an <h3> with the note title and a <button type="button"> whose aria-label is "Delete note: " followed by that exact title (the visible text can be just "Delete").
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
← Previous: Validating input and handling errors on the server