question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to override scroll restoration without conditionally rendering the `ScrollRestoration` component

See original GitHub issue

What version of Remix are you using?

1.4.0

Steps to Reproduce

Issue is reproduced here: https://codesandbox.io/s/remix-unwanted-scrolling-ljr1vb

  1. Scroll down to the bottom of the page where there is a button and a link
  2. Click on either the button or the link

Expected Behavior

The button and the link click is reflected in the the search param of the page’s URL, and the user can continue to interact with the link or button.

Actual Behavior

The click is reflected in the page’s URL but the page scrolls back tot he top, so the user has to scroll back down to continue interacting with the link or button.

This is admittedly a very contrived example, but I recently ran into this in a more realistic situation: while working on a page that has a weekly calendar as a small portion of the page. The user can change weeks by clicking on a next week or previous week button and we wanted to reflect the user’s week in a search param. Each time the user changes weeks however, the page scroll back to the top.

This problem is discussed in issue #186 and several people reported being frustrated by it. The discussion resulted in a couple of suggested fixes:

  • Conditionally rendering the ScrollRestoration component
  • A small modification to the ScrollRestoration component suggested by @wladiston here which would allow individual links or calls to modify the page location to override/disable the scroll restoration

That issue was closed as it was only tangentially related to the actual bug. So I’d like to revive the discussion about incorporating @wladiston solution into Remix’s ScrollRestoration component. There was interest from numerous people and it does seem much more workable than conditionally rendering the component.

I’m not certain whether the proposed solution is the best and clearest. I’ll open a PR with it, but I welcome any suggestions about to approach this better.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:11
  • Comments:5

github_iconTop GitHub Comments

3reactions
jedzejcommented, Sep 5, 2022

I also found this issue very frustrating in a scenario where I control modal visibility via search params. Every time I open the modal, the page scrolls to the top.

0reactions
megawebmastercommented, Oct 13, 2022

In case anyone finds this issue as I did: there is an open PR that has a bit of code helping with scroll restoration issues: https://github.com/remix-run/remix/pull/2812

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing scroll restoration in ecommerce React apps
An advanced tutorial for scroll restoration in React apps, with an example build tailored specifically for ecommerce sites.
Read more >
How do people handle scroll restoration with react-router v4?
scrollRestoration is just a way of disabling the browser's automatic attempts at scroll restoration, which mostly don't work for single-page ...
Read more >
ScrollRestoration v6.6.0 - React Router
This component will emulate the browser's scroll restoration on location changes after loaders have completed to ensure the scroll position is restored to...
Read more >
React 2021 useEffect cleanup & Scroll Restoration - Episode 19
... and useEffect - the useEffect cleanup function - scroll restoration - and more conditional component rendering Finished code from video: ...
Read more >
Scroll in Next js : r/nextjs - Reddit
I config " scrollRestoration: true " in my prj of Next Js . But when i ... and then re-render again so nextjs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found