My Setup
I am using react router 4 in my application and am trying to achieve something along the lines of adding hashes to certain routes on pages, that will save the state of the page so user can link to said state.
So for example - I have products I am listing that have many variants, when the user selects a variant, I add a hash + the variant ID to the URL, so like myapp.com/cars/truck
turns to myapp.com/cars/truck#red
, this allows users to link to that specific variant, which is what I want.
My problem
My problem arises when the user navigates to a page and selects a few variants, then tries to navigate backward. Because I change the hash, each back step hits the previous variant in the browsers history. So if
- the user lands on home
myapp.com/
- navigates to
myapp.com/cars/truck
- then selects the red variant =>
myapp.com/cars/truck#red
, - then blue
myapp.com/cars/truck#red
=>myapp.com/cars/truck#blue
When they press the back button on the browser they will go back to myapp.com/cars/truck#red
, when the desired effect is they go back to the previous page omitting the variant and hash changes, which would be myapp.com/
.
I understand this is the browsers behavior, but what I am wondering is if there is a way to let users link directly to the variant links, and also have it so pressing the back button after the described scenario takes the user back to the previous page (in that scenario it's myapp.com/
). Perhaps re-thinking how I am doing this, I am open to any suggestions.
I am using react (16) and react router (v4) for this if this helps. Happy to show any code if needed, please let me know. Thanks for reading!