TExcelTAD
Home › React › React Mini Project

React Mini Project

Let's put the whole course together and build a small kanban board — the kind of task board teams use to track work. Tasks live in three columns (To do, Doing, Done), can be added, moved, filtered and deleted, and are saved in the browser so they survive a reload.

We'll build it in five steps. Each step is a complete, working app — run it, play with it, then read what changed.

Step Feature Concepts
1 Static board components, props, lists
2 Moving tasks state, events, immutable updates
3 Adding tasks controlled form, validation
4 Reducer + filters useReducer, derived data
5 Persistence & polish useEffect, custom hook, useMemo

Step 1: A static board

Start by splitting the UI into components and rendering hard-coded data. Think of the component tree first: App → Board → Column → TaskCard.

jsx · try it yourself
const COLUMNS = [
  { id: 'todo', title: 'To do' },
  { id: 'doing', title: 'Doing' },
  { id: 'done', title: 'Done' },
];

const TASKS = [
  { id: 1, title: 'Sketch the layout', status: 'done' },
  { id: 2, title: 'Build the Column component', status: 'doing' },
  { id: 3, title: 'Add drag handles', status: 'todo' },
  { id: 4, title: 'Write tests', status: 'todo' },
];

function TaskCard({ task }) {
  return <div style={{ background: 'white', borderRadius: 6, padding: 8, marginBottom: 6, boxShadow: '0 1px 2px #0002' }}>{task.title}</div>;
}

function Column({ title, tasks }) {
  return (
    <section style={{ background: '#eef1f5', borderRadius: 8, padding: 8, width: 180 }}>
      <h3 style={{ margin: '0 0 8px' }}>{title} <small>({tasks.length})</small></h3>
      {tasks.map((t) => <TaskCard key={t.id} task={t} />)}
    </section>
  );
}

export default function App() {
  return (
    <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
      {COLUMNS.map((col) => (
        <Column key={col.id} title={col.title} tasks={TASKS.filter((t) => t.status === col.id)} />
      ))}
    </div>
  );
}

Notice that each task stores its status, and each column derives its tasks with filter. There's one list of tasks — a single source of truth.

Step 2: Moving tasks between columns

Now the tasks must change, so they move into state in App. Cards get ◀ / ▶ buttons; the click travels up through an onMove callback prop, and App creates a new array with the updated task:

jsx · try it yourself
import { useState } from 'react';

const COLUMNS = [
  { id: 'todo', title: 'To do' },
  { id: 'doing', title: 'Doing' },
  { id: 'done', title: 'Done' },
];
const ORDER = COLUMNS.map((c) => c.id);

const INITIAL = [
  { id: 1, title: 'Sketch the layout', status: 'done' },
  { id: 2, title: 'Build the Column component', status: 'doing' },
  { id: 3, title: 'Add drag handles', status: 'todo' },
  { id: 4, title: 'Write tests', status: 'todo' },
];

function TaskCard({ task, onMove }) {
  const i = ORDER.indexOf(task.status);
  return (
    <div style={{ background: 'white', borderRadius: 6, padding: 8, marginBottom: 6, boxShadow: '0 1px 2px #0002' }}>
      <div>{task.title}</div>
      <button disabled={i === 0} onClick={() => onMove(task.id, ORDER[i - 1])}>◀</button>{' '}
      <button disabled={i === ORDER.length - 1} onClick={() => onMove(task.id, ORDER[i + 1])}>▶</button>
    </div>
  );
}

function Column({ title, tasks, onMove }) {
  return (
    <section style={{ background: '#eef1f5', borderRadius: 8, padding: 8, width: 180 }}>
      <h3 style={{ margin: '0 0 8px' }}>{title} <small>({tasks.length})</small></h3>
      {tasks.map((t) => <TaskCard key={t.id} task={t} onMove={onMove} />)}
    </section>
  );
}

export default function App() {
  const [tasks, setTasks] = useState(INITIAL);

  function moveTask(id, status) {
    setTasks(tasks.map((t) => (t.id === id ? { ...t, status } : t)));
  }

  return (
    <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
      {COLUMNS.map((col) => (
        <Column key={col.id} title={col.title} tasks={tasks.filter((t) => t.status === col.id)} onMove={moveTask} />
      ))}
    </div>
  );
}

Step 3: Adding tasks

A controlled form adds new tasks to To do. We trim the input, refuse empty titles and duplicates, and show a short message when something is wrong:

jsx · try it yourself
import { useState } from 'react';

const COLUMNS = [
  { id: 'todo', title: 'To do' },
  { id: 'doing', title: 'Doing' },
  { id: 'done', title: 'Done' },
];
const ORDER = COLUMNS.map((c) => c.id);

function TaskCard({ task, onMove }) {
  const i = ORDER.indexOf(task.status);
  return (
    <div style={{ background: 'white', borderRadius: 6, padding: 8, marginBottom: 6, boxShadow: '0 1px 2px #0002' }}>
      <div>{task.title}</div>
      <button disabled={i === 0} onClick={() => onMove(task.id, ORDER[i - 1])}>◀</button>{' '}
      <button disabled={i === ORDER.length - 1} onClick={() => onMove(task.id, ORDER[i + 1])}>▶</button>
    </div>
  );
}

function NewTaskForm({ onAdd, existingTitles }) {
  const [title, setTitle] = useState('');
  const [error, setError] = useState('');

  function handleSubmit(e) {
    e.preventDefault();
    const clean = title.trim();
    if (!clean) return setError('Please type a title.');
    if (existingTitles.includes(clean.toLowerCase())) return setError('That task already exists.');
    onAdd(clean);
    setTitle('');
    setError('');
  }

  return (
    <form onSubmit={handleSubmit} style={{ marginBottom: 10 }}>
      <input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="New task…" />{' '}
      <button>Add</button>
      {error && <span style={{ color: 'crimson', marginLeft: 8 }}>{error}</span>}
    </form>
  );
}

export default function App() {
  const [tasks, setTasks] = useState([{ id: 1, title: 'Try adding a task', status: 'todo' }]);
  const [nextId, setNextId] = useState(2);

  function addTask(title) {
    setTasks([...tasks, { id: nextId, title, status: 'todo' }]);
    setNextId(nextId + 1);
  }

  function moveTask(id, status) {
    setTasks(tasks.map((t) => (t.id === id ? { ...t, status } : t)));
  }

  return (
    <div>
      <NewTaskForm onAdd={addTask} existingTitles={tasks.map((t) => t.title.toLowerCase())} />
      <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
        {COLUMNS.map((col) => (
          <section key={col.id} style={{ background: '#eef1f5', borderRadius: 8, padding: 8, width: 180 }}>
            <h3 style={{ margin: '0 0 8px' }}>{col.title}</h3>
            {tasks.filter((t) => t.status === col.id).map((t) => <TaskCard key={t.id} task={t} onMove={moveTask} />)}
          </section>
        ))}
      </div>
    </div>
  );
}

The form keeps its own state for the input text and error — nobody else needs them. Only the finished title is sent up through onAdd.

Step 4: A reducer and filters

With adding, moving, deleting and clearing, App is collecting many update functions. A reducer gathers them in one tested place. We also add a search box and a priority filter; the visible tasks are derived from state during render:

jsx · try it yourself
import { useReducer, useState } from 'react';

const COLUMNS = [
  { id: 'todo', title: 'To do' },
  { id: 'doing', title: 'Doing' },
  { id: 'done', title: 'Done' },
];
const ORDER = COLUMNS.map((c) => c.id);
const PRIORITY_COLOR = { low: '#8bc34a', medium: '#ffb300', high: '#e53935' };

function tasksReducer(state, action) {
  switch (action.type) {
    case 'added':
      return { nextId: state.nextId + 1, tasks: [...state.tasks, { id: state.nextId, title: action.title, priority: action.priority, status: 'todo' }] };
    case 'moved':
      return { ...state, tasks: state.tasks.map((t) => (t.id === action.id ? { ...t, status: action.status } : t)) };
    case 'deleted':
      return { ...state, tasks: state.tasks.filter((t) => t.id !== action.id) };
    case 'clearedDone':
      return { ...state, tasks: state.tasks.filter((t) => t.status !== 'done') };
    default:
      return state;
  }
}

const INITIAL = {
  nextId: 4,
  tasks: [
    { id: 1, title: 'Plan sprint', priority: 'high', status: 'done' },
    { id: 2, title: 'Fix login bug', priority: 'high', status: 'doing' },
    { id: 3, title: 'Update docs', priority: 'low', status: 'todo' },
  ],
};

function TaskCard({ task, dispatch }) {
  const i = ORDER.indexOf(task.status);
  return (
    <div style={{ background: 'white', borderRadius: 6, padding: 8, marginBottom: 6, borderLeft: `4px solid ${PRIORITY_COLOR[task.priority]}` }}>
      <div>{task.title}</div>
      <button disabled={i === 0} onClick={() => dispatch({ type: 'moved', id: task.id, status: ORDER[i - 1] })}>◀</button>{' '}
      <button disabled={i === ORDER.length - 1} onClick={() => dispatch({ type: 'moved', id: task.id, status: ORDER[i + 1] })}>▶</button>{' '}
      <button onClick={() => dispatch({ type: 'deleted', id: task.id })} title="Delete">🗑</button>
    </div>
  );
}

export default function App() {
  const [state, dispatch] = useReducer(tasksReducer, INITIAL);
  const [title, setTitle] = useState('');
  const [priority, setPriority] = useState('medium');
  const [search, setSearch] = useState('');
  const [onlyHigh, setOnlyHigh] = useState(false);

  const visible = state.tasks.filter(
    (t) => t.title.toLowerCase().includes(search.toLowerCase()) && (!onlyHigh || t.priority === 'high')
  );

  return (
    <div>
      <form onSubmit={(e) => { e.preventDefault(); if (title.trim()) dispatch({ type: 'added', title: title.trim(), priority }); setTitle(''); }}>
        <input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="New task…" />{' '}
        <select value={priority} onChange={(e) => setPriority(e.target.value)}>
          <option value="low">low</option><option value="medium">medium</option><option value="high">high</option>
        </select>{' '}
        <button>Add</button>
      </form>
      <p>
        <input value={search} onChange={(e) => setSearch(e.target.value)} placeholder="🔍 Search" />{' '}
        <label><input type="checkbox" checked={onlyHigh} onChange={(e) => setOnlyHigh(e.target.checked)} /> high priority only</label>{' '}
        <button onClick={() => dispatch({ type: 'clearedDone' })}>Clear done</button>
      </p>
      <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
        {COLUMNS.map((col) => {
          const colTasks = visible.filter((t) => t.status === col.id);
          return (
            <section key={col.id} style={{ background: '#eef1f5', borderRadius: 8, padding: 8, width: 180 }}>
              <h3 style={{ margin: '0 0 8px' }}>{col.title} <small>({colTasks.length})</small></h3>
              {colTasks.map((t) => <TaskCard key={t.id} task={t} dispatch={dispatch} />)}
              {colTasks.length === 0 && <small style={{ color: 'gray' }}>Nothing here</small>}
            </section>
          );
        })}
      </div>
    </div>
  );
}

The nextId counter now lives inside the reducer's state, so the reducer stays pure — the same state and action always give the same result.

Step 5: Persistence and polish

Finally, the board should remember its tasks. A custom hook usePersistentReducer wraps useReducer: it loads the initial state from localStorage (lazily, once) and saves after every change. All storage access is wrapped in try/catch, because storage can be unavailable — in private windows, or in sandboxed frames like this playground. We also add a progress bar computed with useMemo:

jsx · try it yourself
import { useEffect, useMemo, useReducer, useState } from 'react';

const STORAGE_KEY = 'exceltad-kanban-v1';
const COLUMNS = [
  { id: 'todo', title: 'To do' },
  { id: 'doing', title: 'Doing' },
  { id: 'done', title: 'Done' },
];
const ORDER = COLUMNS.map((c) => c.id);
const PRIORITY_COLOR = { low: '#8bc34a', medium: '#ffb300', high: '#e53935' };

const INITIAL = {
  nextId: 4,
  tasks: [
    { id: 1, title: 'Plan sprint', priority: 'high', status: 'done' },
    { id: 2, title: 'Fix login bug', priority: 'high', status: 'doing' },
    { id: 3, title: 'Update docs', priority: 'low', status: 'todo' },
  ],
};

function tasksReducer(state, action) {
  switch (action.type) {
    case 'added':
      return { nextId: state.nextId + 1, tasks: [...state.tasks, { id: state.nextId, title: action.title, priority: action.priority, status: 'todo' }] };
    case 'moved':
      return { ...state, tasks: state.tasks.map((t) => (t.id === action.id ? { ...t, status: action.status } : t)) };
    case 'deleted':
      return { ...state, tasks: state.tasks.filter((t) => t.id !== action.id) };
    case 'reset':
      return INITIAL;
    default:
      return state;
  }
}

function usePersistentReducer(reducer, initial, key) {
  const [state, dispatch] = useReducer(reducer, initial, (init) => {
    try {
      const saved = localStorage.getItem(key);
      return saved ? JSON.parse(saved) : init;
    } catch {
      return init;
    }
  });

  const [saved, setSaved] = useState(true);
  useEffect(() => {
    try {
      localStorage.setItem(key, JSON.stringify(state));
      setSaved(true);
    } catch {
      setSaved(false);
    }
  }, [key, state]);

  return [state, dispatch, saved];
}

function TaskCard({ task, dispatch }) {
  const i = ORDER.indexOf(task.status);
  return (
    <div style={{ background: 'white', borderRadius: 6, padding: 8, marginBottom: 6, borderLeft: `4px solid ${PRIORITY_COLOR[task.priority]}` }}>
      <div style={{ textDecoration: task.status === 'done' ? 'line-through' : 'none' }}>{task.title}</div>
      <button disabled={i === 0} onClick={() => dispatch({ type: 'moved', id: task.id, status: ORDER[i - 1] })}>◀</button>{' '}
      <button disabled={i === ORDER.length - 1} onClick={() => dispatch({ type: 'moved', id: task.id, status: ORDER[i + 1] })}>▶</button>{' '}
      <button onClick={() => dispatch({ type: 'deleted', id: task.id })} title="Delete">🗑</button>
    </div>
  );
}

function Progress({ tasks }) {
  const percent = useMemo(() => {
    if (tasks.length === 0) return 0;
    return Math.round((tasks.filter((t) => t.status === 'done').length / tasks.length) * 100);
  }, [tasks]);
  return (
    <div style={{ margin: '8px 0' }}>
      <div style={{ background: '#ddd', height: 10, borderRadius: 5, width: 300, overflow: 'hidden' }}>
        <div style={{ width: `${percent}%`, height: '100%', background: '#4caf50', transition: 'width .3s' }} />
      </div>
      <small>{percent}% done</small>
    </div>
  );
}

export default function App() {
  const [state, dispatch, saved] = usePersistentReducer(tasksReducer, INITIAL, STORAGE_KEY);
  const [title, setTitle] = useState('');
  const [priority, setPriority] = useState('medium');

  function handleSubmit(e) {
    e.preventDefault();
    if (!title.trim()) return;
    dispatch({ type: 'added', title: title.trim(), priority });
    setTitle('');
  }

  return (
    <div>
      <form onSubmit={handleSubmit}>
        <input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="New task…" />{' '}
        <select value={priority} onChange={(e) => setPriority(e.target.value)}>
          <option value="low">low</option><option value="medium">medium</option><option value="high">high</option>
        </select>{' '}
        <button>Add</button>{' '}
        <button type="button" onClick={() => dispatch({ type: 'reset' })}>Reset board</button>
      </form>
      <Progress tasks={state.tasks} />
      <div style={{ display: 'flex', gap: 10, alignItems: 'flex-start' }}>
        {COLUMNS.map((col) => {
          const colTasks = state.tasks.filter((t) => t.status === col.id);
          return (
            <section key={col.id} style={{ background: '#eef1f5', borderRadius: 8, padding: 8, width: 180 }}>
              <h3 style={{ margin: '0 0 8px' }}>{col.title} <small>({colTasks.length})</small></h3>
              {colTasks.map((t) => <TaskCard key={t.id} task={t} dispatch={dispatch} />)}
            </section>
          );
        })}
      </div>
      <small style={{ color: 'gray' }}>{saved ? '💾 Saved in this browser' : '⚠️ Storage unavailable here — changes last until you re-run'}</small>
    </div>
  );
}

Ideas to extend it

  • Edit titles in place: double-click a card to show an input (a new edited action).
  • Due dates with a red badge for overdue tasks.
  • Drag and drop with the HTML Drag and Drop API (draggable, onDragStart, onDrop).
  • Undo: keep a history array in the reducer state and add an undo action.
  • Context: provide dispatch through context instead of passing it to every card.
  • TypeScript: type Task, Status and the action union, as shown in React with TypeScript.
  • Tests: unit-test tasksReducer with Vitest — it's a pure function.

Tip: Building the same small app several times with increasing structure (plain state → reducer → custom hook) is one of the best ways to understand why each React tool exists.

Test yourself

Why does each Column receive tasks via tasks.filter(t => t.status === col.id) instead of keeping its own list?

  1. Filtering is faster than state
  2. So there is a single source of truth: one task list, with columns derived from it
  3. Columns can't have state
  4. Because keys require it

Why is nextId stored in the reducer state in Step 4?

  1. Reducers can't read constants
  2. To keep the reducer pure: the same state and action always produce the same result
  3. To make the ids random
  4. Because useState is not allowed with useReducer

Why is localStorage access wrapped in try/catch?

  1. It's always asynchronous
  2. Storage may be unavailable or full, and accessing it can throw
  3. JSON.stringify never throws
  4. React requires it