Skip to content

Intermediate · Building the frontend · topic 13 of 34 · 30 min

Components and props: building the notes list

Explanation

In React, a component is a function that returns a piece of the screen. Props are the inputs you pass to it, like arguments to a function. You build the notes page by putting small components inside bigger ones, and you show a list by mapping each note to a NoteCard with a stable key.

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".

javascript
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>
  );
}
A NoteCard component (JSX). It receives a note and a function as props.

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.

Think of it like this: Think of a chai stall. The order slip says "2 cutting chai, less sugar". The person making tea follows the slip but never rewrites it. Props are that slip: the parent writes it, the child follows it. If the customer wants a change, they tell the counter (call a function prop), and a new slip comes.

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.

output
NotesPage
  SearchBar
  NoteList
    NoteCard (one per note)
  NewNoteButton
The component tree of the notes page. Data flows down through props.

Rendering 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.

javascript
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>
  );
}
key={note.id}: the database id is unique and never changes, so it is the right key.
Careful: Do not use the array index as the key when items can be added, deleted or re-sorted. If you delete the first note, every index shifts, and React may keep the wrong text in an input or the wrong "expanded" state on a card. Use note.id.

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).

Careful: React has an escape hatch called dangerouslySetInnerHTML that inserts raw HTML. Never use it with text a user typed, such as a note body. If you ever must show user HTML (for example rich text), clean it first with a trusted sanitiser library such as DOMPurify.
Tip: Keep the markup semantic: a list of notes is a <ul> with <li> items, a note title is a heading, and every button has a clear text label such as "Delete note: Physics formulas" (use aria-label when the visible text is only an icon or a short word). Screen readers use these to explain the page.

Watch the lesson

Components and props: building the notes list · 1/6

Frontend in React 19 Step 1: components

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)

Task 1 · Code · Javascript

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 "".

Task 2 · Code · Javascript

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.

Task 3 · Code · HTML

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

Question 1

1.What is a React component?

Question 2

2.A NoteCard wants the note to be deleted. What should it do?

Question 3

3.Which key is best for each NoteCard in a list that can be re-sorted and edited?

Question 4

4.A user saves a note titled <script>alert(1)</script>. What happens when React shows {note.title}?

Question 5

5.What is composition in React?

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