Mosh React Tutorial (part 11)

source

Paginating the Data

So the concept is that we should add a property showingArr to our state.
showingArr holds the items to be shown according to what pagination page we’re on.

For example, if we’re on page 1, it should show items 1-4
If its page 2, 5-8
page 3, 9-12, and so on.

What we’re interested in is the first and last item #.
The last item # is always the current page * 4.

So if we’re on page 1, the last item is 4, page 2, last item is 8…etc.

so last item is currentPage * 4

The first item will always be last time – 4

Thus, using them, we paginate the movies array like so:

We create a private function paginateMovies.

In the beginning all the state will be set up when the virtual DOM is being built. When it finishes, it will call render which shows the default state, then in componentDidMount, we will set our state’s showingArr to the correct array. Hence in our componentDidMount():

the setState will trigger a second render, which then will show the paginated items.

full source (movies.jsx)