Skip to content

QuizNot attempted

Spot the bug: comparing pages to items

95 rows, 20 per page, currentPage starts at 1 and totalItems is data.length. When does loading stop?
if (currentPage < totalItems && !isFetching) {
/* load page currentPage + 1 */
}
{currentPage < totalItems && (
<tfoot><tr><td>{isFetching ? 'loading...' : ''}</td></tr></tfoot>
)}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Only when currentPage reaches 95, so it keeps "loading" empty pages long after all rows are shown
  • The condition compares a page count with a row count.
  • After page 5 every row is shown, but 5 < 95, so each intersection schedules another one-second load of an empty slice, and since re-observing a visible element fires straight away it loops while the bottom is on screen.
  • Derive the end in one unit: hasMore = displayData.length < data.length (or currentPage < Math.ceil(total / perPage)), and stop observing when it is false.
  • Not after page 5: that is what the author meant, not what the code checks.
  • Not after page 4: the bug loads too much, not too little.
  • Not immediately: 1 < 95 is true.