Skip to content

Different Scroll Restoration Behaviors for Same Path #10614

Closed Answered by brophdawg11
yuki2006 asked this question in Q&A
Discussion options

You must be logged in to vote

getKey receives the entire location, so it also has access to location.state - which would be anything you pass to <Link state>.

preventScrollReset does indeed avoid scrolling back to 0 if no restored scroll position can be found for the getKey result for that location.

You can combine these accordingly. First, set up your app to restore based on location.pathname for this specific path and location.key everywhere else. But we'll also look at location.state to see if this should opt out entirely.

<ScrollRestoration getKey={(location) => {
  // If the location.state has noRestore, just return a new random key every time so it never restores
  if (location.state?.noRestore) { 
    return Math.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@yuki2006
Comment options

@brophdawg11
Comment options

Answer selected by yuki2006
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants