0

I have infinite scroll feature to load more data.
So when user scroll towards the bottom of the page, below code updates the URL like:

  • /in/top-wear.html?page=2
  • /in/top-wear.html?page=3
    . . . and so on.

But the problem is that if user presses the browser back button then it leaves
the page /top-wear.html instead of going back to previous page like ?page=3 ?page=2 ?page=1 etc.

    useEffect(() => {
        console.log("🐇 top", categoryLoading);
        const handleScroll = () => {
            if(categoryLoading) return;
            const sumOfInnerHeightAndScrollY = (window.innerHeight + window.scrollY);
            const offsetHeight = document.body.offsetHeight;
            const scrollBottomPercentage = ((offsetHeight-sumOfInnerHeightAndScrollY)/offsetHeight)*100;

            if (scrollBottomPercentage < 25) {
                const searchParams = new URLSearchParams(window.location.search);
                const currentPage = parseInt(searchParams.get('page') || 1);
                const nextPage = currentPage + 1;

                console.log("🐇 scroll bottom", paginationButton);

                searchParams.set('page', nextPage);

               
                
                history.push({ search: searchParams.toString() }); //"page=2 and so on..."
            }
        };

        window.addEventListener('scroll', handleScroll);

        return () => {
            window.removeEventListener('scroll', handleScroll);
        };
    }, [history, categoryLoading]);    
    

I am NOT able to find what's the problem. Please help here.
Note:
I just want that if user is currently in page=4 and now user pressed the browser back button, then it should go to page=3 and so on...

5
  • history back button functionality is not working properly, because re-render component many times for infinite scroll functionlity, please check the component rendering Commented Jul 10 at 4:49
  • @UmeshSingh, I have already added categoryLoading validation. If API is already running then it will not go further. Commented Jul 10 at 4:51
  • and, in Firefox browser, it is working fine. Commented Jul 10 at 4:52
  • Using hooks for Chrome browser (Browser suport hooks) automation typically involves utilizing libraries like Puppeteer or Playwright in a Node.js environment. These libraries allow for programmatic control of the Chrome browser to perform tasks such as navigating pages, clicking elements, and scraping data I looking this for nextjs application maybe short this issue Commented Jul 10 at 5:08
  • I debug the code and found one solution, I have added a custom button having click function window.history.back(). Now I just want that whenever user presses the browser back button, then it should click that button programmatically. Commented Jul 10 at 6:34

0

Browse other questions tagged or ask your own question.