react-infinite icon indicating copy to clipboard operation
react-infinite copied to clipboard

reset scroll position when coming back from details page

Open chandu0101 opened this issue 10 years ago • 10 comments

how can i reset scroll position when coming back from details page of item ?

chandu0101 avatar Jul 25 '15 03:07 chandu0101

currently i am adding a class name to scrollable node , on backbutton click i am preserving scroll position in state and on coming back in component did update getting node by classname and setting scroll position stored previously , i don't know whether its okay to do like this or not ...

it will be nice if we have resetScrollPosition function

resetScrollPosition : function(newScrollTop) {
 this.refs.scrollable.getDOMNode().scrollTop = newScrollTop
} 

chandu0101 avatar Jul 27 '15 16:07 chandu0101

The idiomatic way of doing this seems to be adding an initialScrollPosition prop rather than the suggested function. In the meantime, you can use the handleScroll prop to get store the previous scroll position and get a reference to the scrolling DOM node to set later.

garetht avatar Jul 27 '15 16:07 garetht

makes sense , initialScrollPosition :+1:

and get a reference to the scrolling DOM node to set later

how can i get this without adding a classname.. , i don't see any method exposed to get ref on fresh render ..

chandu0101 avatar Jul 27 '15 17:07 chandu0101

It's true it isn't possible to get it on the first render, but temporarily, for this specific use case where you only need to reset the position after the user has scrolled at least once, the DOM node is provided as the first argument to the function you pass to handleScroll.

garetht avatar Jul 27 '15 17:07 garetht

hmm i can hold ref to DOM node on handleScroll ,when details component loaded listview (Infinite) is un mounted ,when coming back to list view(it will be rerendered again with previous state) , prev DOM node ref is not useful anymore! ..

chandu0101 avatar Jul 27 '15 17:07 chandu0101

Ah okay I don't think it'll be possible until the initialScrollPosition change is made.

garetht avatar Jul 27 '15 17:07 garetht

classname trick working fine for me now , i'll update my code when initialScrollPosition landed .. , btw thanks alot for great component :)

Side note : If possible try to follow the prop names similar to https://facebook.github.io/react-native/docs/listview.html ;)

Edit : ah i forgot use case which actually needs resetScrollPosition(..) i have search box and listview when user scolled down in list view and then if he focus on search box i want to scroll to top . In this scenario i prefer exposing a mounted method resetScrollPosition over initialScrollPosition prop . may be we can have both! :)

Edit2: above use case can make it work via handleScroll but again resetScrollPosition(..) looks cleaner!

chandu0101 avatar Jul 27 '15 17:07 chandu0101

Any update on this? initialScrollPosition would be a very nice feature, especially for integrating something like an infinite calendar that can scroll both in the past and future

clauderic avatar Jan 29 '16 14:01 clauderic

Unfortunately not yet, but an issue has come up in our work that may need this, and I'll see if I can get time scheduled to add it.

garetht avatar Feb 03 '16 15:02 garetht

initialScrollPosition would be sweet and much needed.

SmboBeast avatar Jul 03 '17 05:07 SmboBeast