Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Change the Data Then Redraw

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, its data-player-id says wei, and indexOfPlayer() finds Wei’s record in players. The handler sets arrived to true. 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 says Arrived: 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 players holds 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 its render(); line, and this time you ran the same experiment on Remove. You deleted the line from the Remove path, saved main.js and reloaded the page. When you click Remove Jonas, his record leaves players, 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 from players. Jonas’s row is gone, the line says Arrived: 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:

Language: JavaScript Filename: 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.

00:00 Priya’s guest list now lives in players. Each click on Check in or Remove finds a record by its ID, changes the array, then calls render() to draw the page again.

00:11 Your code draws the new page, and the browser shows it only once the click’s handler has finished, so you never see the moments in between. Here, you’ll follow one click through those steps, slowed down for viewing.

00:24 Then you’ll see what the page shows when a handler skips the last one.

00:30 Click Check in Wei. The click starts at the button and climbs to the list, where its one listener runs. The closest method finds the button, and the ID it carries is Wei’s.

00:42 Then indexOfPlayer searches the array and finds Wei’s record. The handler sets that record’s arrived field to true and logs Wei’s name. Look at the page.

00:54 Wei’s row is still white. So far, only the data has changed.

01:00 Next, render() runs, the handler’s last step. First, replaceChildren empties the list. Then its loop draws a new row for each record, in order: Amara, Jonas, Wei and Sam.

01:16 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 says Arrived: 2, Wei and his guest Lin.

01:30 The bar fills to 40%, two of the five people expected.

01:36 The old rows are gone, even Amara’s, which didn’t change. Every call draws the whole list again from whatever players holds right now. Once the handler finishes, the browser shows only that finished list.

01:51 Now skip that last step. In the last lesson, you took the render() call out of Check in. This time, run the same experiment on Remove. In the Remove path, delete it.

02:02 Save and reload. Click Check in Wei again, and the line says Arrived: 2. Then click Remove Jonas. His record leaves the array, but his row stays on the page.

02:17 The bar still shows two of five, though only four people are expected now. Don’t click the buttons in his row. They point at a record that’s gone, so a second Remove Jonas would remove Sam’s record instead.

02:31 Check in still calls render() at its end. Your turn: pause the video and predict whether Jonas’s row is still on the page after you click Check in Sam.

02:46 Click Check in Sam. Slowed down again, the list empties and is drawn again from the records. Jonas’s row is gone.

02:57 The line says Arrived: 3, and the bar is three quarters full, three of the four people expected. The redraw came from players, and Jonas’s record had already left.

03:09 The page was only behind.

03:13 So the data decides what the page shows, but only once your code draws the page again. Check in and Remove both follow the same three steps. Find the record by its ID.

03:25 Change the array. Call render() to draw the page. Put that call back in the Remove path before you go on. Each of those steps starts from the ID a button carries.

03:37 Next, in Give Each Record a Stable Id, you’ll look at what makes a good one.

Become a Member to join the conversation.