T
TanStack3mo ago
other-emerald

Don't `resetScroll` on the same page

The default resetScroll behavior is somewhat annoying when you have buttons/links on the same page that simply change some URL state. Is there a way to set up our router so that resetScroll's default behavior is true when going to a different page, but false when going to the same page (i.e. just changing URL params)?
3 Replies
flat-fuchsia
flat-fuchsia3mo ago
there is no builtin mechanism. but you could wrap Link to supply resetScroll:false
manual-pink
manual-pink2mo ago
If you need to navigate without resetting the scroll, you can specify what constitutes a new page using the getScrollRestorationKey function on the createRouter function. The example provided in the docs shows how you would do this such that only changing pathname resets the scroll and is what I typically do in most applications https://tanstack.com/router/latest/docs/framework/react/guide/scroll-restoration#custom-cache-keys:~:text=You%20could%20sync%20scrolling%20to%20the%20pathname Not explicitly what you have asked for, but may be what you want
other-emerald
other-emeraldOP2mo ago
Ahh thanks, this looks great!

Did you find this page helpful?