Replace Navigation History Items with Gatsby’s Link Component

Jason Lengstorf
InstructorJason Lengstorf

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 2 years ago

In some cases, navigating to a new page should replace the current page, meaning the “back” button will skip the current URL. For example, if a page leads to an interstitial screen, such as a confirmation page, it may be desirable to skip that confirmation if the user clicks the “back” button after confirming. In this video, you’ll learn how Gatsby’s Link component manages replacing navigation history using the replace prop.

Instructor: [00:01] In some cases, we don't want to keep every page that we visit in history. For example, if I choose waffles, I'm asked to confirm my choice. Once I confirm, I see what I've chosen. Then I'm told to press the back button to keep making decisions. If I hit back, I go back to this confirmation page, but what I really want is to be on the decision page.

[00:21] To fix that, we can use the replace prop in Gatsby's Link component. The way that that will happen is on the confirm page, where we have this Link prop, we can add replace. Adding the replace prop means that when we navigate to choice, instead of adding it as the next page we visited, it will replace the confirm page entirely in history.

[00:45] We can see how this works by going back out and choosing pancakes. Yes, I'm sure. Now, when we hit back, it goes straight to decisions. There aren't a lot of reasons why you would do this, but in the case of something like a confirmation page, it can make sense. It's helpful to know how it works.