Different Scroll Restoration Behaviors for Same Path #10614
-
Hello, I am using the ScrollRestoration component from React Router in my application and I have a specific requirement related to scroll restoration behavior. I have a path (let's say /path1) in my application which can be accessed from multiple links. However, I want these links to exhibit different scroll restoration behaviors:
All of these links lead to the same path (/path1), so differentiating them based on the URL isn't an option. I also want to avoid changing the URL (adding query parameters) to differentiate the links. Is there a way in React Router to handle this use case? I'd like to have only one ScrollRestoration component in my root component. Thanks in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You probably want a combination of |
Beta Was this translation helpful? Give feedback.
getKey
receives the entirelocation
, so it also has access tolocation.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 thegetKey
result for that location.You can combine these accordingly. First, set up your app to restore based on
location.pathname
for this specific path andlocation.key
everywhere else. But we'll also look atlocation.state
to see if this should opt out entirely.