Apply the whole process to a task board: hierarchy, static version, minimal state, where it lives, and callbacks back up.
What to build
A three-column task board where cards move between columns.
Requirements
- Columns
To do,DoingandDonearesectionelements with the classcolumnand the attributedata-status(planned,doing,done), each with anh2showing the name and the number of cards, asTo do (2). - Each card is an
articlewith the classcardshowing its title in aspan, with abutton←(not in To do) and abutton→(not in Done) that move it one column. - A form with an
inputwith the idnew-taskand a submitbuttonAddadds a card to To do (ignoring empty titles). The board starts withWrite reportin To do,Fix loginin Doing andPlan sprintin To do.
Constraints
The only state is the list of tasks (and the new task's text); columns and counts are computed.
Example
Moving Write report right, and adding "Email Ada", gives To do (2), Doing (2) and Done (0).
Why it works
Every column and count is derived from one list of tasks, so a card can never be in two columns, and the cards report moves to App through callbacks, which is the inverse data flow of the process.
Hints
Hint 1
Hierarchy: App (state) > Board > Column > Card. The only state is the list of tasks, each with a status.
Hint 2
Moving a card changes its status to the previous or next of ["planned", "doing", "done"].
Solution
Show one correct solution (try it yourself first)
import { useState } from "react";
const STATUSES = ["planned", "doing", "done"];
const NAMES = { planned: "To do", doing: "Doing", done: "Done" };
let nextId = 4;
function Card({ task, onMove }) {
const at = STATUSES.indexOf(task.status);
return (
<article className="card">
<span>{task.title}</span>
{at > 0 && <button onClick={() => onMove(task.id, -1)}>←</button>}
{at < STATUSES.length - 1 && <button onClick={() => onMove(task.id, 1)}>→</button>}
</article>
);
}
function Column({ status, tasks, onMove }) {
return (
<section className="column" data-status={status}>
<h2>
{NAMES[status]} ({tasks.length})
</h2>
{tasks.map((t) => (
<Card key={t.id} task={t} onMove={onMove} />
))}
</section>
);
}
function Board({ tasks, onMove }) {
return (
<div className="board">
{STATUSES.map((s) => (
<Column key={s} status={s} tasks={tasks.filter((t) => t.status === s)} onMove={onMove} />
))}
</div>
);
}
export default function App() {
const [tasks, setTasks] = useState([
{ id: 1, title: "Write report", status: "planned" },
{ id: 2, title: "Fix login", status: "doing" },
{ id: 3, title: "Plan sprint", status: "planned" },
]);
const [title, setTitle] = useState("");
function move(id, by) {
setTasks(tasks.map((t) => (t.id === id ? { ...t, status: STATUSES[STATUSES.indexOf(t.status) + by] } : t)));
}
function handleSubmit(event) {
event.preventDefault();
if (title.trim() === "") return;
setTasks([...tasks, { id: nextId++, title: title.trim(), status: "planned" }]);
setTitle("");
}
return (
<div>
<form onSubmit={handleSubmit}>
<input id="new-task" value={title} onChange={(e) => setTitle(e.target.value)} />
<button type="submit">Add</button>
</form>
<Board tasks={tasks} onMove={move} />
</div>
);
}