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

List State: collection.getItem() can return undefined #5776

Open
cedeber opened this issue Jan 30, 2024 · 5 comments
Open

List State: collection.getItem() can return undefined #5776

cedeber opened this issue Jan 30, 2024 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cedeber
Copy link
Contributor

cedeber commented Jan 30, 2024

Provide a general summary of the issue here

Well. I have a very hard to reproduce bug - it's randomly happening - and hard to check in the current repo the returned type. So, I build a Combobox thanks to the hooks and load async some data to fill the items - with or without useAsyncList btw - and sometimes I receive:

Uncaught TypeError: Cannot read properties of undefined (reading 'index')
    at useListState.ts:81:21

so looks like that here

const startItem = cachedCollection.current.getItem(selectionState.focusedKey);
collection.getItem() can return undefined

🤔 Expected Behavior?

It doesn't crash ^_^

😯 Current Behavior

It does crash :-/

💁 Possible Solution

No response

🔦 Context

No response

🖥️ Steps to Reproduce

It's a very hard issue to reproduce and hard to have an easy example unfortunately.
I can't have a setup with the current reop that returns another type that "any", so it's really hard to find out what happens.

The only thing I know it that you need to:

  • open the combobox
  • put some text (not necessarily needed)
  • select an item
  • it reloads the items list because filterText changed (set by the selection)
  • clean the selected Item
  • it reloads the first 50 items (filterText = "")
  • repeat

as fast as you can to trigger it. I feel like checking the possible returned value of getItem would be easier but I was unsuccessful here.

Version

latest

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Windows

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

@LFDanLu
Copy link
Member

LFDanLu commented Jan 30, 2024

Just to make sure, you are just using the hooks in your implementation right? No RAC components? I ask because RAC has a different collections setup than what we had before.

Its hard for me to say what is occurring here without a reproduction I can dig into on my end, are you able to add breakpoints to find out what the collection and cached collection is? We could always add some more resiliency against the collection.getItem() returning a undefined but ideally that section of the code should always have a cached collection in which the previous focused key exists in.

@Develekko
Copy link

i have the same issue

@Develekko
Copy link

i think they need add
let index = Math.min( ( diff > 1 ? Math.max(startItem?.index - diff + 1, 0) : // Optional chaining startItem?.index // Optional chaining ), itemNodes.length - 1);

@mkrystkowicz
Copy link

Same problem on me, looks like overwriting package locally, and making all startItem variables optionally chained solves the issue. Not sure if owners of the package will accept that PR, but I can maybe fix that in my free time.

const startItem = cachedCollection.current.getItem(selectionState.focusedKey);

☝🏻 collection can be undefined when I spam on useAnycList items - set, delete, change quickly and the error is thrown. No issue when optionally chained

@LFDanLu LFDanLu added bug Something isn't working help wanted Extra attention is needed labels May 13, 2024
@LFDanLu
Copy link
Member

LFDanLu commented May 13, 2024

Adding the optional chaining sounds good to me, but just to double check: where does focus land with the above change? Does it get lost to the document body? Or does it land on the list itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
Status: ✏️ To Groom
Development

No branches or pull requests

4 participants