🎓 A1Learner
🔍
0 of 50 lessons completed

Capstone: A Task App

A task app joins lists, state, events, conditional rendering, and forms into one small app. The app adds tasks, marks tasks done, and filters the list.

Add a task

Three dots copy the tasks and add the new task.

const [tasks, setTasks] = useState([]);
const [text, setText] = useState('Buy milk');
const next = [...tasks, { id: 1, text: text, done: false }];
return <p>{next.length} task.</p>;

Mark a task done

Map copies each task and flips done for the matching id.

const tasks = [{ id: 1, text: 'Buy milk', done: false }];
const next = tasks.map((task) => (task.id === 1 ? { ...task, done: true } : task));
return <p>{next[0].done ? 'Done.' : 'Waiting.'}</p>;

Example

import { useState } from 'react';

export default function App() {
  const [tasks, setTasks] = useState([]);
  const [text, setText] = useState('');
  const [filter, setFilter] = useState('all');

  function addTask(e) {
    e.preventDefault();
    if (text === '') {
      return;
    }
    setTasks([...tasks, { id: tasks.length + 1, text: text, done: false }]);
    setText('');
  }

  function toggleTask(id) {
    setTasks(tasks.map((task) => (task.id === id ? { ...task, done: !task.done } : task)));
  }

  const shown = tasks.filter((task) => {
    if (filter === 'active') { return !task.done; }
    if (filter === 'done') { return task.done; }
    return true;
  });

  return (
    <div>
      <h1>Tasks</h1>
      <form onSubmit={addTask}>
        <input value={text} onChange={(e) => setText(e.target.value)} />
        <button type="submit">Add</button>
      </form>
      <button onClick={() => setFilter('all')}>All</button>
      <button onClick={() => setFilter('active')}>Active</button>
      <button onClick={() => setFilter('done')}>Done</button>
      {shown.length === 0 ? <p>No tasks yet.</p> : (
        <ul>
          {shown.map((task) => (
            <li key={task.id}>
              <button onClick={() => toggleTask(task.id)}>{task.done ? 'Done' : 'Do it'}</button>
              {task.text}
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}

Key points

  • A tasks state value holds an array of task objects.
  • An input state value holds the text of the new task.
  • The add handler builds a new task and clears the input.
  • The toggle handler flips the done value of one task.
  • A filter state value picks all, active, or done tasks.
  • The list shows a message when no tasks match the filter.

In this example, the tasks state value starts as an empty array. The text state value holds the new task text. The filter state value starts as all. The add handler stops the reload, builds a task, and clears the input. The toggle handler flips done for the matching id. The shown list filters tasks by the filter value. The first render shows No tasks yet because the list is empty.

Note

The task list, the input, and the filter each keep their own state value.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does the toggle handler change in the task app?