To ensure that the view moves to the first post when clicking on the next and previous icons in the pagination, you can use JavaScript to scroll the page to the top. Here’s how you can update your code to achieve this:
const handlePreviousPage = () => {
if (currentPage > 1) {
setCurrentPage(currentPage - 1);
window.scrollTo(0, 0); // Scroll to the top of the page
}
};
const handleNextPage = () => {
if (currentPage < totalPages) {
setCurrentPage(currentPage + 1);
window.scrollTo(0, 0); // Scroll to the top of the page
}
};
- When clicking on the previous or next icons,
handlePreviousPageandhandleNextPagefunctions are called, respectively. - Inside these functions,
window.scrollTo(0, 0)is used to scroll the page to the top (horizontal and vertical coordinates both set to 0), ensuring that the view moves to the first post.