Mosh React Tutorial (part 10)

download source

Handling Pagination Changes

When we click on each pagination, let’s make that pagination index active.

First, let’s put a callback for when the pagination happens.

movies.jsx
In the render function:

The handler itself:

update our state:

Whenever a pagination is clicked, it calls the movies component to update and set its state’s currentPage.

Hence, in our Pagination, we get the currentPage successfully. We do UI updates, so let’s put the class ‘active’ in our HTML whenever the currentPage matches our current displayed page.

When we loop through each pagination page number to be displayed, if that page number equals our currentPage, then we put the active class. Else, don’t put it. That way, we highlight the page that we’re on.

You should now see the highlighted pagination