user173420
user173420

Reputation: 376

React pagination component

Its a movie search/finder, with pagination component.

The pagination currently doesn't work as aspected.

Sorry about the codepen, I'm new to react and I'm a bit lost.

Upvotes: 0

Views: 480

Answers (2)

Coffezilla
Coffezilla

Reputation: 386

Just use react-bhx-pagination to do this easily passing a json or the endpoint. Everything will work

Repo: react-bhx-pagination

Example: Online Demo

Upvotes: 0

Giovanni Esposito
Giovanni Esposito

Reputation: 11176

There is a problem on your useEffect. You wrote:

useEffect(() => {
    getMovies();
  }, [pageNo]);

But should be:

useEffect(() => {
   getMovies(keyword, pageNo);
}, [pageNo, keyword]);

Why? Because every time you change pageNo, useEffect will be fired but if you don't pass to the function the new page value, getMovies will return always with films of the first page.

Then, of course, pageNo should start from 1:

const [pageNo, setPageNo] = useState(1);

And of course you should change this div (is not necessary to do pageNo - 1)

<div className="pagination-current">
Page {pageNo} of {maxPages}
</div>

Here your codepen modified.

Upvotes: 1

Related Questions