Think of a component as a cookie cutter
One cutter, many cookies. TaskItem is the cutter for “one task row”. Props are what makes each cookie different: one row says “Buy milk”, the next says “Learn React”. Same shape, different data.
Step 1: Describe what a task looks like
Create src/types.ts. This is the shape of one task, written once with TypeScript and reused by every component. (We'll use Filter in lesson 5.)
// What one task looks like. Every component that uses tasks imports this.
export type Task = {
id: number;
title: string;
done: boolean;
};
export type Filter = "all" | "active" | "done";Step 2: Your first component with props
Create a folder src/components and a file TaskItem.tsx:
type Props = {
title: string;
done: boolean;
};
// One task row. It gets its data from props, like a function gets arguments.
export function TaskItem({ title, done }: Props) {
return (
<li className={done ? "task done" : "task"}>
<label>
<input type="checkbox" checked={done} readOnly />
<span>{title}</span>
</label>
</li>
);
}Three things to notice:
type Propslists the inputs. TypeScript will complain if someone forgets one or passes the wrong type.{ title, done }unpacks the props object, so you can writetitleinstead ofprops.title.className={done ? "task done" : "task"}picks a CSS class from the data. Done tasks get crossed out by the stylesheet.
Step 3: Use it, many times
Now replace src/App.tsx and use the component three times with different props:
import { TaskItem } from "./components/TaskItem";
export default function App() {
return (
<main className="app">
<h1>My To-Do List</h1>
<ul className="task-list">
<TaskItem title="Buy milk" done={false} />
<TaskItem title="Learn React" done={true} />
<TaskItem title="Call mum" done={false} />
</ul>
</main>
);
}Save. You'll see three tasks, with “Learn React” crossed out. You wrote the row once and used it three times. That's the whole point of components.
Props flow one way: down
| Props | State (next lesson) | |
|---|---|---|
| Who owns it | The parent | The component itself |
| Can it change? | Not by the child: read-only | Yes, with its setter function |
| Direction | Parent → child | Stays inside, but can be passed down as props |
| To-Do example | Each TaskItem's title and done | App's list of tasks |
Right now the tasks are typed into App by hand, and the checkbox is readOnly because nothing can change yet. In the next lesson we add state, so you can type new tasks into the app.
Where component files go
src/
├── App.tsx ← the top component
├── types.ts ← shared types (Task)
└── components/ ← one file per component
└── TaskItem.tsxKey takeaways
- ✓Components let you build a piece of UI once and reuse it with different data.
- ✓Props pass data from parent to child; inside, unpack them with { }.
- ✓TypeScript types make each component's inputs clear and checked.
- ✓Props can't be changed by the child; changing data needs state.
- ✓Our To-Do app now shows a list of TaskItem rows, typed in by hand for now.
Frequently asked questions
What is a component in React?
A function whose name starts with a capital letter and returns JSX. You use it like an HTML tag: <TaskItem />. Each component is one reusable piece of the screen.
What are props in React?
Props are the inputs of a component, written like HTML attributes: <TaskItem title="Buy milk" done={false} />. Inside the component they arrive as one object, usually unpacked with { title, done }.
Can a component change its own props?
No. Props are read-only, like the ingredients someone hands you. If something needs to change, it should be state (next lesson), owned by the component that changes it.
Why do strings use quotes but numbers and booleans use { }?
Quotes pass a plain string: title="Buy milk". Anything else is JavaScript, so it goes in braces: done={false}, count={3}, task={task}.
Should I put each component in its own file?
Yes, one component per file in src/components, named after the component (TaskItem.tsx). Export it with export function TaskItem and import it where you need it.
🧠 Quick check: components and props
1. How do you pass the number 3 as a prop called count?
2. Inside TaskItem, how do you read the title prop?
3. A child wants to change a prop it received. What should happen?
4. Which component name is valid?
Checking your access…