Home > Software design >  Redirect to first page if user refresh the browser in ReactJS
Redirect to first page if user refresh the browser in ReactJS

Time:10-26

I have a URL for product page like http://localhost:3000/product?page=1 and at the bottom of the page there is a button Load More. If we click that button, it will change the URL http://localhost:3000/product?page=2 (without reloading the page) and show the products for the page.
I want that if user refresh the browser and page>1 then user should redirect to page=1. i.e; We don't allow page refresh if page > 1
Basically, question is about to detect page reload.

I tried :

if (window.performance) {
  if (PerformanceNavigationTiming.type == 1) {
    alert( "This page is reloaded" );
  } else {
    alert( "This page is not reloaded");
  }
}

But this did not work for me.

CodePudding user response:

All you want to do is something when the page component mounts for the first time, for example setting the page number back to 1. So on the page component inside useEffect(()=>{ //do whatever you want,like setting the page no to 1 },[]) - which will be called once every time when the page mounts. Hope this helps!!

  • Related