Next.js Discord

Discord Forum

How to Navigate to the Page the User Clicked the Cart Button On

Unanswered
Jersey Wooly posted this in #help-forum
Open in Discord
Jersey WoolyOP
I know history is a complex matter, but please hear my problem out before embarking on complex answers. I have a Cart button in the header of my root layout. The Cart page has a link that should return to the page the user was on when they clicked the cart button.

The cart page currently has a button with text "Back to Shop" and a hard-coded url for the shop route. I would like to have that button dynamically navigate back to the page the user was previously on. It needn't be via an anchor as it is currently, it could even be an event and a function call if necessary.

I've found how to use the useRouter hook to navigate to the previous page, but the cart may have more than one page, and I don't want my button to navigate to the previous cart page, but back to the page where the user clicked the Cart button.

1 Reply

Dutch
Best is hardcode /shop i guess