Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement pagination in this project? #31

Open
SimilArity opened this issue May 28, 2022 · 7 comments
Open

How to implement pagination in this project? #31

SimilArity opened this issue May 28, 2022 · 7 comments

Comments

@SimilArity
Copy link

Hi everyone,

I started learning Next JS and GraphQL with this project so I'm a beginner. I wanted to implement pagination a few days ago and still to this day, I'm lost.

Did someone manage to achieve that?

@SimilArity
Copy link
Author

Hi everyone,

I'm still trying to do the pagination. Could someone help me with this:

This is my main index.js : https://controlc.com/32243536

And this is my index.js (services : https://controlc.com/511f7e29

Should I use useEffect or something like useSWR?

I'm stuck for a long time now. I hope someone will see this and kindly guide me ...

@SimilArity
Copy link
Author

Am I the only one right here who want to implement pagination on this blog? :/

@wanyama413
Copy link

Hi, I want to implement pageanation too but yet to figure it out

@arlbdt
Copy link

arlbdt commented Sep 8, 2022

I used this : https://www.npmjs.com/package/react-paginate

The code i used:

function Posts({currentPosts}) {
    return (
      <>
        {currentPosts &&
          currentPosts.map((post, index) => (
            <PostCard key={index} post={post.node} />
          ))}
      </>
    );
  }

  function PaginatedPosts({itemsPerPage}) {
    // We start with an empty list of items.
    const [currentPosts, setCurrentPosts] = useState(null);
    const [pageCount, setPageCount] = useState(0);
    // Here we use item offsets; we could also use page offsets
    // following the API or data you're working with.
    const [itemOffset, setItemOffset] = useState(0);

    useEffect(() => {
      // Fetch items from another resources.
      const endOffset = itemOffset + itemsPerPage;
      console.log(`Loading items from ${itemOffset} to ${endOffset}`);
      setCurrentPosts(posts.slice(itemOffset, endOffset));
      setPageCount(Math.ceil(posts.length / itemsPerPage));
    }, [itemOffset, itemsPerPage]);

    // Invoke when user click to request another page.
    const handlePageClick = e => {
      const newOffset = (e.selected * itemsPerPage) % posts.length;
      console.log(
        `User requested page number ${e.selected}, which is offset ${newOffset}`
      );
      setItemOffset(newOffset);
    };

    return (
      <>
        <Posts currentPosts={currentPosts} />
        <ReactPaginate
          nextLabel="next >"
          previousLabel="< previous"
          onPageChange={handlePageClick}
          pageRangeDisplayed={3}
          marginPagesDisplayed={2}
          pageCount={pageCount}
          pageClassName="page-item"
          pageLinkClassName="page-link"
          previousClassName="page-item"
          previousLinkClassName="page-link"
          nextClassName="page-item"
          nextLinkClassName="page-link"
          breakLabel="..."
          breakClassName="page-item"
          breakLinkClassName="page-link"
          containerClassName="pagination"
          activeClassName="active"
          renderOnZeroPageCount={null}
        />
      </>
    );
  }`

Then insert the component:

<PaginatedPosts itemsPerPage={5} />

@arlbdt
Copy link

arlbdt commented Sep 8, 2022

You will have to do some css to customize it (use the classnames of the ReactPaginate component)

@arlbdt
Copy link

arlbdt commented Sep 8, 2022

Create a component and reuse it for the categorie page aswell

@alexbennycodes
Copy link

Infinite Scroll seems like a better option rather than pagination.

React Infinite Scroll - I have used this npm package for some of my projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants