Here's what you'll build: a task list (a todo list) with a text box and an Add button, a checkbox on every row, a Delete button, three filter buttons (All, Active, Done), and a line underneath that says "1 not done yet". Each task in the list is called a todo.
A todo app like this is a good first project, because nearly everything you've learned on the React track is used together: state, events, lists with key, conditional rendering and components. A todo app is like a shopping list stuck on the fridge. There's only one sheet of paper: you add items, cross out what you've bought, remove what you no longer need, and sometimes you only read what isn't crossed out yet. "What isn't crossed out" isn't a second sheet, it's a way of reading the same one. In the todo app, that sheet of paper is one piece of state, todos. Crossing out an item is ticking a todo, and reading what isn't crossed out is choosing the Active filter.
Setting up the project
A new React project is usually made with Vite, from the terminal:
npm create vite@latest todo-app -- --template react
cd todo-app
npm install
npm run dev
npm create vite@latestmakes atodo-appfolder holding a React project that's ready to run. Even with--template react(the React starting setup), Vite still asks two things: a linter (Oxlint is fine) andInstall with npm and start now?. Answer No so that you run the next three commands yourself. If you answer Yes, Vite installs the libraries and starts the server, so skipcd,npm installandnpm run dev.npm installdownloads every library the project needs into thenode_modulesfolder.npm run devstarts the development server (a server for trying the app on your computer). The terminal shows an address likehttp://localhost:5173. Open it in the browser. Every time you save a file, the page changes too.
The libraries a project uses (called dependencies) and its commands are listed in package.json. Here's a short version:
{
"name": "todo-app",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"react": "^19.2.0",
"react-dom": "^19.2.0"
}
}
npm run dev runs the command named dev under "scripts". Older projects made with Create React App use npm start. Create React App has been deprecated since February 2025, so do not use it for new projects.
Plan first: components, state, hierarchy
Before you write code, plan in three steps. They are a short form of "Thinking in React", a five-step guide on react.dev:
- Break the UI into components. The text box and the Add button become
TodoForm, the filter buttons becomeFilterBar, each row becomesTodoItem, andAppwraps them all. - Decide the state. State is data that changes and has to be remembered. Here there are only three pieces: the
todoslist, the text box's contenttext, and the chosenfilter. Put each piece in the component that uses it. If several components use it, put it in the closest parent that wraps them all (lifting state up). - Build the hierarchy and the data flow. The hierarchy is how the components sit: which wraps which. Data goes down from parent to child through props. Changes come up through functions passed as props: the child calls one, the parent changes its state.
The result can be written as a diagram:
App state: todos, filter
├─ TodoForm state: text props: onAdd
├─ FilterBar props: filter, onChange
└─ TodoItem (one per todo) props: todo, onToggle, onDelete
Only TodoForm uses text, so that state lives there. The list that's shown and the "not done yet" counter use todos, and FilterBar and that list use filter, so both live in App.
Three changes, each one a new array
Start with two todos, then do three things in a row:
| What you do | todos afterwards |
|---|---|
| Start | Buy rice (not done), Wash the motorbike (done) |
| Add "Learn React" | Buy rice (not done), Wash the motorbike (done), Learn React (not done) |
| Tick "Buy rice" | Buy rice (done), Wash the motorbike (done), Learn React (not done) |
| Delete "Wash the motorbike" | Buy rice (done), Learn React (not done) |
This is the whole app. The three changes in the table are made by addTodo, toggleTodo and deleteTodo:
import { useState } from "react"
const FILTERS = { all: "All", active: "Active", done: "Done" }
function TodoForm({ onAdd }) {
const [text, setText] = useState("")
const add = () => {
if (!text.trim()) return // ignore an empty entry
onAdd(text.trim())
setText("") // empty the text box
}
return (
<div>
<input value={text} onChange={(e) => setText(e.target.value)} />
<button onClick={add}>Add</button>
</div>
)
}
function FilterBar({ filter, onChange }) {
return (
<div>
{Object.entries(FILTERS).map(([value, label]) => (
<button key={value} disabled={value === filter} onClick={() => onChange(value)}>
{label}
</button>
))}
</div>
)
}
function TodoItem({ todo, onToggle, onDelete }) {
return (
<li>
<input type="checkbox" checked={todo.done} onChange={() => onToggle(todo.id)} />
{todo.text}
<button onClick={() => onDelete(todo.id)}>Delete</button>
</li>
)
}
export default function App() {
const [todos, setTodos] = useState([
{ id: 1, text: "Buy rice", done: false },
{ id: 2, text: "Wash the motorbike", done: true },
])
const [filter, setFilter] = useState("all")
const addTodo = (text) => setTodos([...todos, { id: Date.now(), text, done: false }])
const toggleTodo = (id) => setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)))
const deleteTodo = (id) => setTodos(todos.filter((t) => t.id !== id))
const visible = todos.filter((t) => (filter === "active" ? !t.done : filter === "done" ? t.done : true))
const left = todos.filter((t) => !t.done).length
return (
<div>
<TodoForm onAdd={addTodo} />
<FilterBar filter={filter} onChange={setFilter} />
<ul>
{visible.map((t) => (
<TodoItem key={t.id} todo={t} onToggle={toggleTodo} onDelete={deleteTodo} />
))}
</ul>
<p>{left} not done yet</p>
</div>
)
}
[...todos, { ... }]makes a new array: everything that was there, plus one todo at the end.Date.now()(the current time in milliseconds) becomes the new todo'sid.todos.map(...)makes a new array of the same length. The todo whoseidmatches is replaced by a new object{ ...t, done: !t.done }, and the rest are used as they are.todos.filter(...)makes a new array without the todo with the deletedid.- Why does it have to be new? React compares the old state with the new one. If you
todos.push(...)and thensetTodos(todos), it's still the same array. React decides nothing changed, so it doesn't re-render. key={t.id}lets React recognise each row by itsid, not by its position. When a row is deleted or filtered out (hidden by the filter), its tick doesn't jump to another row.Object.entries(FILTERS)turns the objectFILTERSinto an array of pairs[value, label], soFilterBarmakes one button per pair. The filter button that is already chosen getsdisabled(it can't be clicked).
What's shown is calculated, not stored
visible (the list that's shown) and left (how many todos are not done yet) aren't stored in state. Both are calculated again from todos and filter every time App renders. In visible, the filter "active" picks the todos where !t.done (not done yet), "done" picks the ones where t.done, and "all" picks them all (true). If you kept the visible list as a second piece of state, every add, tick and delete would have to change two pieces of state. Forget once, and the two no longer match.
With the data in the table above, after adding "Learn React" and ticking "Buy rice": todos holds 3 todos, the Active filter shows 1 (Learn React), and left is 1. The two finished todos are still in todos, they just aren't shown.
⚠️ Small slips that break a todo app
- Changing the array or object in place. If you write
todos.push(newTodo)ortodo.done = true, then call the setter, the data changes, but the screen doesn't. Always make a new array or object with spread,maporfilter. - The index as
key. Withkey={i}(the row's position number), a tick the browser keeps (a box withoutchecked) can stick to the wrong row when a todo is deleted or filtered out. Useid. - Forgetting to empty the text box. After adding, call
setText(""). In the exercise, the checker tests this: after the button is pressed, the text box has to be empty again. - Letting an empty entry in. Check
text.trim()first, so an entry of only spaces doesn't become a todo.
TL;DR: Make the project with npm create vite@latest, then run it with npm run dev. Its dependencies (the libraries the project uses) and scripts (the project's commands) are listed in package.json. Plan in three steps: break the UI into components, decide the state and which component it lives in, then build the hierarchy (how the components sit). Data goes down through props, and changes come up through functions passed as props. A todo app (a task list) needs only three pieces of state: todos (the list of todos), text (the text box's content) and filter (the chosen filter). Add a todo with [...todos, newTodo], tick it with map, and delete it with todos.filter: all three make a new array. Calculate the visible list from state on every render instead of storing it twice.
⚡ Plan first
Write:
App state: todos, filter
├─ TodoForm state: text props: onAdd
├─ FilterBar props: filter, onChange
└─ TodoItem (one per todo) props: todo, onToggle, onDelete
Guess first: the diagram above is made before you write the code of a React project. What are the planning steps?
Break the UI into components, decide the state, then build the hierarchy and the data flow. These three steps are a short form of "Thinking in React", a guide on react.dev.
Meaning: This diagram is the plan of a todo app (a task list). App wraps TodoForm (the text box and the Add button), FilterBar (the filter buttons), and one TodoItem (one row) per todo. text (the text box's content) is only used by TodoForm, so that state lives there. todos (the list of todos) and filter (the chosen filter) are used by several components, so both live in App.
⚡ Three changes, all new arrays
Write:
const addTodo = (text) => setTodos([...todos, { id: Date.now(), text, done: false }])
const toggleTodo = (id) => setTodos(todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)))
const deleteTodo = (id) => setTodos(todos.filter((t) => t.id !== id))
Meaning: These three lines change todos, the state that holds the list of todos. Each todo is an object with an id, a text and done (finished or not). t is one todo. Adding uses [...todos, ...]. Ticking uses map, which flips done on the todo whose id matches. Deleting uses filter. All three make a new array, so React knows something changed.
⚡ Data goes down, changes go up
Meaning: App holds the state todos and wraps TodoItem (one todo row), so App is its parent. App sends data down through props, such as one todo to TodoItem. A child sends changes up by calling a function from its props. For example, TodoItem calls onDelete(todo.id), then App deletes that todo from the state.