Mohsen007
Mohsen007

Reputation: 285

Back to previous page instead of previous url in next js

I faced a problem where I have some filters on a page. When you filter, the page URL will change and the query string will add to the end of the URL.So when you click on the back button, the page will stay on the current page and will back to the previous query string. What I want is to get back to the previous page which is come from that to the current page. For example, I am on /home page, then I go to /browse page, then I filter some items and my URL will /browse?subject=2 and I filter again and the URL will be changed to /browse?subject=2&vendor=2. Now I want to the previous page (I mean /home), when I click on the back button I will go to /browse?subject=2. I hope I have conveyed what I mean.

Upvotes: 1

Views: 6321

Answers (1)

Taghi Khavari
Taghi Khavari

Reputation: 6582

What you need to do is in your /browse page when you are filtering use router.replace instead of router.push in this way, your filter result won't be added into the browser history and when you try to use router.back you would go to the previous page.

Upvotes: 2

Related Questions