In this tutorial, I want to show you how to use React Table Library with its usePagination plugin to implement pagination. In the previous example, you installed React Table Library to create a table component. Now, you will enable your users to paginate pages in a table.
First, import the usePagination hook:
Second, initialize it with the tableâs data, plus an initial page number and page size, and pass it as a plugin prop to the Table component:
And third, create your Pagination component (or inline HTML elements as in this example) and interact with the pagination feature programmatically by reading from its pagination state and writing to its pagination state with its functions:
Thatâs it. With just a few lines, you have pagination working in the table. Letâs now create a notifier to get pages from the table. Letâs see how this works with the usePagination hook:
The onChange callback function gives you access to the action which triggered the pagination change and to the current pagination state of your table (in this example, its page number and page size). With access to this information, you can trigger further table or non-table events (e.g. a side effect such as server-side pagination) based on it.
In addition, it is worth noting that the pagination object that you passed to the table is packed with the pagination state â which gives you the ability to access it at any time â and to all the functions to paginate the table programmatically .
You have only seen one possible version of pagination for the table. As you have full access to the pagination state and its functions, you can create your own version of it as well. Find more pagination examples in the documentation .