In this video, you followed one Check in Wei click through Priya’s code, step by step, and then saw what the page shows when a handler skips render(). Here are the key points:
- A click changes the data first. The click on Check in Wei climbs to the list, where the one listener runs.
closest()finds the button, itsdata-player-idsayswei, andindexOfPlayer()finds Wei’s record inplayers. The handler setsarrivedtotrue. At that point, only the data has changed, and Wei’s row is still white. Your code draws the new page, and the browser shows it only once the handler has finished, so you never see that moment. The video slowed it down for viewing. render()draws the whole list again.replaceChildren()empties the list, and the loop draws a new row for each record, in order. Wei’s record says he’s arrived, so his new row is green, with its Check in button disabled. Along the way,render()adds up the parties. The line saysArrived: 2, Wei and his guest Lin, and the bar fills to 40%, two of the five people expected.- Every row is new after a redraw. The old rows are gone, even Amara’s, which didn’t change. Every call draws the list from whatever
playersholds right now. Once the handler finishes, the browser shows only that finished list. - Without
render(), the page falls behind the data. In the last lesson, Check in lost itsrender();line, and this time you ran the same experiment on Remove. You deleted the line from the Remove path, savedmain.jsand reloaded the page. When you click Remove Jonas, his record leavesplayers, but his row stays on the page. The bar still shows two of five, though only four people are expected. - A stale row’s buttons point at a record that’s gone. Don’t click the buttons in Jonas’s row. Their record has left
players, so a second Remove Jonas would remove Sam’s record instead. - The next redraw catches up. Check in still calls
render(), so clicking Check in Sam draws the list again fromplayers. Jonas’s row is gone, the line saysArrived: 3, and the bar is three quarters full, three of the four people expected. The page was only behind. - The data decides what the page shows, but only once your code draws it. Check in and Remove, which act on guests already on the list, both follow the same three steps:
- Find the record by its ID.
- Change the array.
- Call
render()to draw the page.
Here’s the list’s listener with both paths following those steps. If you deleted the Remove path’s render(); to try it, put it back:
scratch/main.js
playerList.addEventListener("click", (event) => {
const removeButton = event.target.closest(".remove");
if (removeButton !== null) {
players.splice(indexOfPlayer(removeButton.dataset.playerId), 1);
render();
return;
}
const button = event.target.closest("[data-player-id]");
if (button === null) {
return;
}
const player = players[indexOfPlayer(button.dataset.playerId)];
player.arrived = true;
console.log("Checked in:", player.name);
render();
});
Each of those steps starts from the ID a button carries. Next, in Give Each Record a Stable Id, you’ll look at what makes a good one.