Replace Navigation History Items with Gatsby’s Link Component

Jason Lengstorf
InstructorJason Lengstorf
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 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.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today