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.

scrollTop not working if rendered with 0 items

See original GitHub issue

Hey,

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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bvaughncommented, Apr 23, 2017

Ah, you are right about the first issue. I misread (distracted working on something else).

The second check can’t be changed, since scrollTop (and scrollLeft) props are meant to function more as… default values, rather than true controlled props. The change you’ve suggested would cause a List 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 the List 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.

0reactions
bvaughncommented, Apr 23, 2017

You’re welcome 😄

Read more comments on GitHub >

github_iconTop 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 >

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