Load-more pagination for the article list

React · React · advanced · greenfield

Adds client-side load-more paging to the article list: slices the visible window from the ordered prop and grows it by a page per click. The button hides itself once everything is on screen, and short final pages come through on the last click.

Requirements

Files touched

--- src/components/ArticleList.jsx
+import React, { useState } from 'react';
+
+const PAGE_SIZE = 10;
+
+export default function ArticleList({ articles }) {
+  const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
+  const visible = articles.slice(0, visibleCount);
+  const hasMore = visibleCount < articles.length;
+
+  if (articles.length === 0) {
+    return <p className="empty">No articles yet.</p>;
+  }
+
+  return (
+    <section>
+      <ul>
+        {visible.map((article) => (
+          <li key={article.id}>{article.title}</li>
+        ))}
+      </ul>
+      {hasMore && (
+        <button type="button" onClick={() => setVisibleCount((count) => count + PAGE_SIZE)}>
+          Load more
+        </button>
+      )}
+    </section>
+  );
+}
+

Review this PR

React practice