Skip to content
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

TypeError: Cannot read properties of undefined (reading 'key') #597

Open
inokawa opened this issue Jan 8, 2025 Discussed in #592 · 10 comments
Open

TypeError: Cannot read properties of undefined (reading 'key') #597

inokawa opened this issue Jan 8, 2025 Discussed in #592 · 10 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@inokawa
Copy link
Owner

inokawa commented Jan 8, 2025

Discussed in #592

Originally posted by mariia-mitiureva December 24, 2024
After updating to 0.39.2 sometimes (quite rare) it crashes with the following error:

image

I happens randomly, not sure what can be a trigger. Did it happen to someone else?

@inokawa inokawa self-assigned this Jan 8, 2025
@inokawa inokawa added the help wanted Extra attention is needed label Jan 8, 2025
@inokawa
Copy link
Owner Author

inokawa commented Jan 8, 2025

It was reported in

@RobHannay
Copy link

We have this too. We can't reliably reproduce it I'm afraid.

For us, it mainly seems to happen after leaving the list open for a while, and then coming back to it (refocussing). I could be wrong though

@aeharding
Copy link
Contributor

I have a reliable reproduction! For me it happens:

  1. On a VList with keepMounted used for sticky items (removing keepMounted and cannot reproduce)
  2. Scroll down so keepMounted is in use
  3. In my app, Voyager, switch accounts (to account on same instance) which effectively just reloads with brand new items.

I believe what's happening is I am setting the keepMounted but it is slightly stale (value is set in effect), so Virtua sees keepMounted index as out-of-bounds and crashes. Essentially, going from list of 40 with keepMounted={[30]} to list of 3 items with same keepMounted until the next react cycle. So it can be fixed by a sanity check:

diff --git a/src/features/community/list/ResolvedCommunitiesList.tsx b/src/features/community/list/ResolvedCommunitiesList.tsx
index e5c39b46..18cb02cd 100644
--- a/src/features/community/list/ResolvedCommunitiesList.tsx
+++ b/src/features/community/list/ResolvedCommunitiesList.tsx
@@ -216,7 +216,9 @@ function ResolvedCommunitiesList({
             updateActiveIndex();
           }}
           as={IonList as CustomContainerComponent}
-          keepMounted={activeIndex >= 0 ? [activeIndex] : []}
+          keepMounted={
+            activeIndex >= 0 && activeIndex < items.length ? [activeIndex] : []
+          }
           item={StickyItem}
         >
           {items.map((item, index) => (

See here for more: aeharding/voyager#1799 (comment)

Not sure if this is the same issue everyone else is experiencing? If so maybe Virtua could add a sanity check and ignore out of bounds keepMounted to avoid crash

@RobHannay
Copy link

Ah yes! Smoking gun. We're also using keepMounted for sticky headers

@inokawa
Copy link
Owner Author

inokawa commented Jan 14, 2025

Thanks for the help! keepMounted has no range check and that's expected one. It's recommend to set it by yourself for now.

Although, I got the same error without keepMounted prop. So I'm still investigating. Still not sure it's related to 0.39.

@mgraczyk
Copy link

mgraczyk commented Feb 5, 2025

FWIW I am seeing this error without using keepMounted. Seems to happen whenever one of the items changes size

@atsui-affinity
Copy link

atsui-affinity commented Feb 14, 2025

I'm seeing this error without using keepMounted as well for Virtualizer. Also, my usecase is when the number of item changes. This problem started happening after version 0.37.0

@miapolis
Copy link

0.37.0 removed the default overscan value of 4

@@ -171,11 +170,11 @@
   {
     children,
     count: renderCountProp,
-    overscan = 4,
-    keepMounted,
+    overscan,

Adding overscan={4} to Virtualizer fixed the issue for me on 0.40.0.

@atsui-affinity
Copy link

0.37.0 removed the default overscan value of 4

@@ -171,11 +170,11 @@
{
children,
count: renderCountProp,

  • overscan = 4,
  • keepMounted,
  • overscan,
    Adding overscan={4} to Virtualizer fixed the issue for me on 0.40.0.

Thank you for the information. I attempted to add overscan={4}, but unfortunately, the error persists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants