How a real app is put together
State, render(), handlers, saving and the server — the five parts of any front end.
Over five chapters you learned each piece on its own: finding elements, catching clicks, reading forms, talking to a server, saving to localStorage. A project is where you have to put it all together so that a week later you're not lost. Every front-end app has five parts:
- state — the data that describes the screen: the array of to-dos, the current filter;
- `render()` — a function that draws the screen from the state;
- handlers — react to clicks and forms: they change the state and call render();
- saving — the state lives in localStorage and survives a reload;
- syncing — the same state is fetched from a server and sent back to it.
const todos = [
{ id: 1, title: "Buy cat food", done: false },
{ id: 2, title: "Nap on the keyboard", done: true },
];
function render() {
const items = todos.map((todo) => {
const li = document.createElement("li");
li.textContent = (todo.done ? "✓ " : "· ") + todo.title;
return li;
});
document.querySelector("#list").replaceChildren(...items);
}
render();
console.log(document.querySelectorAll("#list li").length);render() turns it into list items. replaceChildren swaps all the old content for the new.A handler never touches the list directly. It does two things: changes the state and calls `render()`. render() handles the rest: draws the items, counts what's left, highlights the filter. The rule sounds boring, but it saves you from half of all bugs.
let todos = [];
let number = 1;
function render() {
const items = todos.map((todo) => {
const li = document.createElement("li");
li.textContent = todo.title;
return li;
});
document.querySelector("#list").replaceChildren(...items);
const left = todos.filter((todo) => !todo.done).length;
document.querySelector("#left").textContent = "Left: " + left;
}
document.querySelector("#add").addEventListener("click", () => {
todos = [...todos, { id: number, title: "Task #" + number, done: false }];
number += 1;
render();
});
render();
document.querySelector("#add").click();
document.querySelector("#add").click();
console.log(document.querySelector("#left").textContent);render(). Press the button on the Page tab — the counter is always honest.const list = document.querySelector("#list");
let left = 0;
function addItem(title) {
const li = document.createElement("li");
li.textContent = title;
list.append(li);
left += 1;
document.querySelector("#left").textContent = "Left: " + left;
}
addItem("Buy cat food");
addItem("Take a nap");
list.firstElementChild.remove(); // removed an item, forgot the counter
console.log(list.children.length, document.querySelector("#left").textContent);Saving and the server fit the same scheme. The state is a plain array, so it's easy to turn into JSON: put it into localStorage after every change and send it to the server with fetch. On start-up, the reverse: read it and call render().
const response = await fetch("/api/todos");
const todos = await response.json();
localStorage.setItem("todos", JSON.stringify(todos));
const saved = JSON.parse(localStorage.getItem("todos"));
console.log(saved.length, saved[0].title);Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.