scrollTop not working if rendered with 0 items
See original GitHub issueHey,
Thanks for the great lib.
I am trying to set the scrollTop prop on a List that is wrapped by an InfiniteScroller. During the first render the list’s rowCount is 0, and after that all scrollTop is ignored, and always stays at 0.
In https://github.com/bvaughn/react-virtualized/blob/master/source/Grid/Grid.js, line 596:
componentWillReceiveProps (nextProps) {
const { scrollLeft, scrollTop } = this.state
if (
nextProps.columnCount === 0 &&
scrollLeft !== 0 ||
nextProps.rowCount === 0 &&
scrollTop !== 0
) {
this._setScrollPosition({
scrollLeft: 0,
scrollTop: 0
})
}
Since nextProps.rowCount === 0, and scrollTop !== 0, _setScrollPosition is setting scrollTop to 0, and now since my scrollTop never changes always staying at a constant number (>0), scrollTop is never set again.
Here is a fiddle: https://jsfiddle.net/omerts/qcrg17md/
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
The same old issue, .scrollTop(0) not working in Chrome ...
I've designed a page that has several fixed elements covering the page and makes of html5/css3 to create a clean "mask" over the...
Read more >2891 - document.documentElement.scrollTop always returns ...
The scrollTop property remains zero and scroll position does not change. Please provide any additional information below. Attach a screenshot if possible.
Read more >Window sizes and scrolling - The Modern JavaScript Tutorial
For instance, if we try to scroll the page with a script in <head> , it won't work. Regular elements can be scrolled...
Read more >ScrollView - React Native
FlatList is also handy if you want to render separators between your items, multiple columns, infinite scroll loading, or any number of ...
Read more >Hiding DOM Elements in React Based on Scrolling
const listenToScroll = () => { let heightToHideFrom = 1000; const winScroll = document.body.scrollTop || document.documentElement.scrollTop; if ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, you are right about the first issue. I misread (distracted working on something else).
The second check can’t be changed, since
scrollTop
(andscrollLeft
) props are meant to function more as… default values, rather than true controlled props. The change you’ve suggested would cause aList
to be snap-locked to the offset specified. (Anytime the state changed in response to a user scroll, the next prop-change would snap it back.)It’s a bit tricky/unfortunate, but I kind of backed myself into a corner with the API design. ☹️
You can use the API method I mentioned above to work around this or you could refrain from setting
scrollTop
until theList
has some rows. Unfortunately I think those are the only 2 options without a major refactor- so I’m going to close this issue for now. Happy to keep discussing it though.You’re welcome 😄