Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nextjs pagination getting error on pathname

using react-paginate package trying to access pathname its showing error Below my code like

const pagginationHandler = (page) => { 
        const currentPath = location.pathname;
        const currentQuery = location.search;
        currentQuery.page = page.selected + 1;
        user.router.push({
            pathname: currentPath,
            query: currentQuery,
        });



<ReactPaginate
                    previousLabel={'previous'}
                    nextLabel={'next'}
                    breakLabel={'...'}
                    breakClassName={'break-me'}
                    activeClassName={'active'}
                    containerClassName={'pagination'}
                    subContainerClassName={'pages pagination'}
    
                    initialPage={user.totalcount.currentPage - 1}
                    pageCount={user.totalcount.pageCount}
                     marginPagesDisplayed={2}
                    pageRangeDisplayed={5}
                    onPageChange={pagginationHandler}
                />

passing All required Value to ReactPaginate and Now getting error from pagginationHandler
TypeError: Cannot create property ‘page’ on string ‘?page=1’
image error screen https://media.todaysprint.com/i/khdJfMW8.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In nextjs, you should use useRouter to get query params

import { useRouter } from 'next/router';

const router = useRouter();
const pagginationHandler = (page) => { 
    const currentPath = router.pathname;
    const currentQuery = { ...router.query };
    currentQuery.page = page.selected + 1;
    router.push({
        pathname: currentPath,
        query: currentQuery,
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading