Skip to content

Todo List App

Easy ~25 min

What you'll build

A todo list app where you can add new items, mark items as complete (with strikethrough), and delete items. Each item has a unique ID generated with Date.now().

Goals

  • Manage array state with useState
  • Render lists with .map() and unique keys
  • Build a controlled input for adding todos
  • Update arrays immutably (spread operator)

Prerequisites

  • useState basics
  • Array methods (map, filter)
  • JSX syntax

Steps

  1. 1

    Set up state and the input form

    Create a TodoList component with a todos array state and a text input state for the new todo.

    Code
    const [todos, setTodos] = useState([]);
    const [text, setText] = useState("");

    Expected result

    Empty state is ready for adding todos.

  2. 2

    Add the addTodo function

    Create a function that adds a new todo object with a unique id, the current text, and completed: false. Clear the input after adding.

    Code
    const addTodo = () => {
      if (!text.trim()) return;
      setTodos([...todos, { id: Date.now(), text, completed: false }]);
      setText("");
    };

    Expected result

    Typing and clicking Add creates a new todo item.

  3. 3

    Render the todo list

    Map over todos and render each with a checkbox, the text, and a delete button. Use the todo id as the key.

    Code
    {todos.map(todo => (
      <li key={todo.id}>
        <input type="checkbox" checked={todo.completed}
          onChange={() => toggleTodo(todo.id)} />
        <span style={{ textDecoration: todo.completed ? "line-through" : "none" }}>
          {todo.text}
        </span>
        <button onClick={() => deleteTodo(todo.id)}>Delete</button>
      </li>
    ))}

    Expected result

    All todos are displayed with checkboxes and delete buttons.

  4. 4

    Implement toggle and delete functions

    toggleTodo maps over todos and flips the completed flag for the matching id. deleteTodo filters out the matching id.

    Code
    const toggleTodo = (id) => {
      setTodos(todos.map(t => t.id === id ? { ...t, completed: !t.completed } : t));
    };
    
    const deleteTodo = (id) => {
      setTodos(todos.filter(t => t.id !== id));
    };

    Expected result

    Checking a checkbox strikes through the text. Delete removes the item.

  5. 5

    Add state management & event handling

    Use useState to manage component state and add onClick/onChange handlers to interactive elements. State is the heart of React components, and event handling lets them respond to user actions.

    Code
    import { useState } from "react";
    
    export function MyComponent() {
      const [count, setCount] = useState(0);
      const [text, setText] = useState("");
    
      return (
        <div>
          <button onClick={() => setCount(c => c + 1)}>
            Count: {count}
          </button>
          <input
            value={text}
            onChange={(e) => setText(e.target.value)}
          />
        </div>
      );
    }

    Expected result

    Clicking the button increments the count; the input content updates in real time as you type.

Try it yourself

What you learned

You practiced immutable array state updates (spread for add, map for toggle, filter for delete), list rendering with keys, and controlled inputs — the core patterns for managing collections in React.

Related Resources

Related Lessons

Related Practice

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic