nextui-org / nextui

🚀 Beautiful, fast and modern React UI library.
https://nextui.org
MIT License
21.41k stars 1.39k forks source link

[BUG] - Using array of strings as AutoComplete items prop throws cryptic TypeError: WeakMap keys must be objects or non-registered symbols #3677

Open MarcusDelvecchio opened 3 weeks ago

MarcusDelvecchio commented 3 weeks ago

NextUI Version

2.4.6

Describe the bug

Just spent a huge amount of time debugging an issue when trying to provide a list of IDs/strings to an autocomplete component and battling this mysterious error. The items prop can apparently be any iterable, but this is not the case and it cannot be an array of strings. I think it would be helpful to fix this or provide a more accurate error message for the AutoComplete component.

The following doesn't work: rendering the items from a list of ids and then using said IDs to render the items.

<Autocomplete
   defaultItems={Object.keys(myDictionary)}
>
      {(dictionaryKey) => <AutocompleteItem key={dictionaryKey} value={dictionaryKey}>{myDictionary[dictionaryKey]}</AutocompleteItem>}
</Autocomplete>

But the following does work: converting the ids to a list of objects and then using them to render the items.

<Autocomplete
   defaultItems={Object.keys(myDictionary).map(dictionaryKey => myDictionary[dictionaryKey])}
>
      {(dictionaryItem) => <AutocompleteItem key={dictionaryItem.id} value={dictionaryItem.id}>{dictionaryItem.name}</AutocompleteItem>}
</Autocomplete>

Keep in mind I am working with a dictionary of {id: value} pairs.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  1. use the component
  2. provide an array of strings as the items / defaultItems prop

Expected behavior

As I user, when passing in an iterable array of string to to the AutoComplete items / defaultItems prop, an error should no longer be thrown so that I can use those items to render my AutoCompleteItems, or a more clear error/documentation should be provided to indicate that this should not be done.

Screenshots or Videos

image

Operating System Version

macOS

Browser

Safari

linear[bot] commented 3 weeks ago

ENG-1289 [BUG] - Using array of strings as AutoComplete items prop throws cryptic TypeError: WeakMap keys must be objects or non-registered symbols

ryo-manba commented 4 days ago

Thanks for the issue!

When passing a function to children, react-aria uses a WeakMap to cache the rendering, which is why you can't pass a list of string. However, using Array.map should work as expected. https://github.com/adobe/react-spectrum/blob/db51509aada412327191b3f03b06449977b309f7/packages/%40react-aria/collections/src/useCachedChildren.ts#L38-L40

Related: https://github.com/nextui-org/nextui/issues/2938