Skip to content

Restore scroll position #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jjvattamattom opened this issue Aug 10, 2015 · 16 comments
Closed

Restore scroll position #1686

jjvattamattom opened this issue Aug 10, 2015 · 16 comments

Comments

@jjvattamattom
Copy link

I want behaviour similar to what the browser offers. If I scrolled down a long page, opened a new page and immediately went back, it would load the previous page at the same scroll position. React Router always re-renders the view and resets scroll, when we go back. How do I get the 'save scroll' with React Router?

@mjackson mjackson changed the title Caching views? Restore scroll position Aug 10, 2015
@mjackson
Copy link
Member

Yes, we have plans to automatically let you restore the scroll position as you navigate back to pages you've already visited. We already have the functionality in master, but we need remix-run/history#17 to be fixed first.

@jjvattamattom
Copy link
Author

Does it cache previously rendered DOM and then restore scroll position? Or is the component mounted again? I would expect scroll position to be meaningless if the component renders differently.

@gaearon
Copy link
Contributor

gaearon commented Aug 11, 2015

It can't (and shouldn't) cache the DOM, after all we're in React's paradigm. Yes, it's up to you to render your components the same way. For example if you don't cache the data locally, there's nothing router can do to restore your position. But that's how browser's default behavior works, too, we're just trying to emulate it.

@antonholmquist
Copy link

How is this planned to work with async loading of data?

Often the view that you navigate back to will be initially empty, waiting for content to load. As soon as the data is loaded, you typically want to reset the scroll position.

Or is there a way to pre-fetch critical data and feed into the view before mount? That would partly solve the issue.

@ryanflorence
Copy link
Member

How is this planned to work with async loading of data?

You need to cache it (or render sever side with it) so when the back button is clicked you get a synchronous render w/ all the data.

@th0r
Copy link

th0r commented Aug 18, 2015

@ryanflorence That's a bad news. Can't we restore scroll position only after rendering?

@gaearon
Copy link
Contributor

gaearon commented Aug 18, 2015

@th0r The router can't know when your data is ready.

@antonholmquist
Copy link

@gaearon If you tell the router, it would now.

I tried Ryan's advice on caching and restoring the data to get the scroll position to restore correctly. Seems to work well.

@th0r
Copy link

th0r commented Aug 18, 2015

@gaearon The router knows, when I render something (it means, I already have all the data I need) and it knows it's current state.
We've already discussed this here: #1158 (comment)

@th0r
Copy link

th0r commented Aug 18, 2015

@antonholmquist What if user goes 5 pages back? How are you dealing with it?

@vinceprofeta
Copy link

@mjackson is there a solution for this? If so where can I find the docs?

@jackyon
Copy link

jackyon commented Feb 29, 2016

also waiting a solution for async loading, any news?

I have tried scroll-behavior, seems this not support async loading, the scrolling position were wrong after I pressed back button.

@KODerFunk
Copy link

I have a problem, correlated with this issue. Help me please! http://stackoverflow.com/questions/36088020/keep-react-router-routed-components-for-all-history-states

@jackyon
Copy link

jackyon commented Mar 21, 2016

loos like this issue was solved after I upgrade to the new version of react simple router, now it change the name to "react-router-redux".

the position now can be remembered

@KODerFunk
Copy link

Scroll position is only one effect, i need render whole history steps.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants