Adding Pagination to a Comments Section with Hasura and React

In this lesson, we'll add pagination to both the hook and UI.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Full Stack Comments with Hasura and React course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Full Stack Comments with Hasura and React, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to Full Stack Comments with Hasura and React

In this lesson, we're going to add pagination. Very often, we don't always want to fetch all the comments. If there are too many of them, the APA call can be slow and worsen the user experience. For that reason, we'll add pagination so that users can decide whether they want to read more comments by going to the next page or not. We'll first extend the hook and then add a simple UI. Let's open the usecommons.ts file. We'll make a few modifications. The usecommons function will accept a new parameter, an object with limit and offset fields. The first one stands for how many comments should be fetched. And the second one stands for where should we start. For example, offset =10 means skip the first 10 comments. This object will be optional as someone may still want to fetch all the comments. As the next step, we'll modify the get comments query to take the limit and offset variables. We also need to pass the variables in a fetch call. We'll add them to the post_ruco's body, but only if they are provided. Whenever offset or limit change, we need to refresh the comments. For example, when a user goes to the next page, the offset changes. We should listen to those changes in our hook and fetch the comments accordingly. For that, we'll pass limit and offset to the dependency array in use effect, so that the fetch_commons function is called whenever those values change. And we're almost there. However, we don't have total comments count when using limit and offset configurations. It's necessary when we want to display the number of pages for user pagination. It's also necessary when we want to show the number of all the comments as we did so far. So, we're going to extend the get_commons query to fetch aggregated count as well. We'll add comments aggregate and we'll pass the where parameter and we'll get the count. Now that we're fetching the count, we need to store it somewhere. We'll add a new state variable and we'll update it after a successful API call. After updating our local comment state, we'll also update the count state. And the last step, we need to return the count variable from the hook. Now let's add a simple UI with previous and next buttons for comments pag ination. Here we'll also perform a few steps. Let's go to the post comments.tsx file. We'll provide the hook with limit and offset variables. For now, we're going to hardcode limit to 10, but feel free to set a different number. We'll store the offset in a state variable and it will be updated upon previous or next button clicks. Since we're now getting the total count as a separate variable from the hook, we'll modify the part responsible for displaying comments number information and use the count from the API. Last and the most significant step is navigation with the previous and next buttons. I will add enough element with a paragraph to display the current page and a div who've set buttons. I won't deter the styles we used here because we talked about Tyler's classes a little earlier and I hope that you're a bit familiar with them. It's also just a simple UI implementation, so feel free to improve and beautify it. For example, one thing that you couldn't do here is using different styles for disabled buttons. When we are on the first or on the last page, previous and the next button should be disabled accordingly. Now let's go to the browser and see how the pagination looks like. So we see the number of comments and the patterns. Let's quickly play and see if that works. We can go to the next page. We can also go back to the previous page. Well done. We are officially done with the hook implementation. Feel free to stop here for a bit, improve the UI, improve the styles. You can also add a configuration for the pagination and maybe you have some other ideas. In the next bonus module, we'll learn how to publish this hook as a library and we'll also add a manual approval for the comments.