petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

[BUG] TypeError A(react-virtuoso/dist/index.m) e.getBoundingClientRect is not a function #687

Closed alexgleason closed 2 years ago

alexgleason commented 2 years ago

Describe the bug Hi there! I'm getting quite a lot of Sentry errors about this line here:

Screenshot from 2022-06-20 11-38-49

Reproduction

My first thought: it must be old browsers, but it's not. Here are some clients where this is failing:

Screenshot from 2022-06-20 11-41-30

Screenshot from 2022-06-20 11-41-41

Meanwhile, should be supported: https://caniuse.com/?search=getBoundingClientRect

To Reproduce

I can't reproduce it locally. Here are arguments from Sentry:

Screenshot from 2022-06-20 11-44-07

Expected behavior

No error is thrown.

petyosi commented 2 years ago

@alexgleason what you're seeing there is the default value of the itemSize property. My guess is that, somehow, this gets called on something that does not have the method - maybe window?

Can you try something like this:

export function customCorrectItemSize(el: HTMLElement, dimension: 'height' | 'width') {
  try {
    return Math.round(el.getBoundingClientRect()[dimension])
  } catch (e) {
   // you might want to throw a more meaningful error, like, getting the element id, or something.
    throw new Error(`${el} does not have getBoundingClientRect`)
  }
}

and then pass it:

<Virtuoso itemSize={customCorrectItemSize} />
petyosi commented 2 years ago

@alexgleason did you find out something here? Curious to solve this.

alexgleason commented 2 years ago

Working on it: https://gitlab.com/soapbox-pub/soapbox-fe/-/merge_requests/1578

Haven't gotten past the local dev stage yet. The Virtuoso component disappears with these errors in the console:

Warning: NaN is an invalid value for the height css style property.

Warning: Received NaN for the data-known-size attribute. If this is expected, cast the value to a string.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 2.16.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

petyosi commented 2 years ago

@alexgleason I think I found the problem (another report pinpointed it).

alexgleason commented 2 years ago

Thank you!